google-nomulus/java/google/registry/mapreduce/UnlockerOutput.java
mcilwain ded40851d3 Use locking on async mapreduces
This ensures that only one will run at a time, which should help fix the
clogged up mapreduces we've seen on sandbox.

In order to do this, the UnlockerOutput is introduced. This unlocks the
given Lock after all reducer shards have finished.

Also increases the lease duration of the DNS refresh action from 20 to
240 minutes. 20 minutes isn't long enough; when there's a lot of domains
and decent system load the mapreduce could take longer than that in the
ordinary case.

TESTED=Deployed to alpha and verified that more than one copy of the
mapreduce wouldn't run simultaneously, and also that the lock is
released when the mapreduce is finished.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=205887554
2018-08-10 13:44:25 -04:00

65 lines
2 KiB
Java

// Copyright 2018 The Nomulus Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package google.registry.mapreduce;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.appengine.tools.mapreduce.Output;
import com.google.appengine.tools.mapreduce.OutputWriter;
import com.google.common.flogger.FluentLogger;
import google.registry.model.server.Lock;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;
/** An App Engine MapReduce "Output" that releases the given {@link Lock}. */
public class UnlockerOutput<O> extends Output<O, Lock> {
private static final long serialVersionUID = 2884979908715512998L;
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Lock lock;
public UnlockerOutput(Lock lock) {
this.lock = lock;
}
private static class NoopWriter<O> extends OutputWriter<O> {
private static final long serialVersionUID = -8327197554987150393L;
@Override
public void write(O object) {
// Noop
}
@Override
public boolean allowSliceRetry() {
return true;
}
}
@Override
public List<NoopWriter<O>> createWriters(int numShards) {
return Stream.generate(NoopWriter<O>::new).limit(numShards).collect(toImmutableList());
}
@Override
public Lock finish(Collection<? extends OutputWriter<O>> writers) {
logger.atInfo().log("Mapreduce finished; releasing lock: %s", lock);
lock.release();
return lock;
}
}