Periodically log transfer progress for RDE SFTP uploads

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=201172975
This commit is contained in:
mcilwain 2018-06-19 07:36:20 -07:00 committed by Ben McIlwain
parent 8b263baefa
commit 47322b7fcd
3 changed files with 97 additions and 4 deletions

View file

@ -0,0 +1,82 @@
// 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.rde;
import com.google.common.collect.ImmutableMap;
import com.google.common.flogger.FluentLogger;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.SftpProgressMonitor;
import google.registry.util.Clock;
import javax.inject.Inject;
import org.joda.time.DateTime;
import org.joda.time.Duration;
/** A progress monitor for SFTP operations that writes status to logs periodically. */
public class LoggingSftpProgressMonitor implements SftpProgressMonitor {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final long LOGGING_CHUNK_SIZE_BYTES = 5 * 1024 * 1024;
private final Clock clock;
private long bytesOfLastLog = 0;
private int callsSinceLastLog = 0;
private DateTime timeOfLastLog;
@Inject
LoggingSftpProgressMonitor(Clock clock) {
this.clock = clock;
}
/** Nice display values for SFTP operation modes. */
private static final ImmutableMap<Integer, String> OPERATION_MODES =
new ImmutableMap.Builder<Integer, String>()
.put(ChannelSftp.OVERWRITE, "OVERWRITE")
.put(ChannelSftp.RESUME, "RESUME")
.put(ChannelSftp.APPEND, "APPEND")
.build();
@Override
public void init(int op, String src, String dest, long max) {
timeOfLastLog = clock.nowUtc();
logger.atInfo().log(
"Initiating SFTP transfer from '%s' to '%s' using mode %s, max size %,d bytes.",
src, dest, OPERATION_MODES.getOrDefault(op, "(unknown)"), max);
}
@Override
public boolean count(long count) {
callsSinceLastLog++;
long bytesSinceLastLog = count - bytesOfLastLog;
if (bytesSinceLastLog > LOGGING_CHUNK_SIZE_BYTES) {
DateTime now = clock.nowUtc();
logger.atInfo().log(
"%,d more bytes transmitted in %,d ms; %,d bytes in total. [%,d calls to count()]",
bytesSinceLastLog,
new Duration(timeOfLastLog, now).getMillis(),
count,
callsSinceLastLog);
bytesOfLastLog = count;
callsSinceLastLog = 0;
timeOfLastLog = now;
}
// True means that the upload continues.
return true;
}
@Override
public void end() {
logger.atInfo().log("SFTP operation finished.");
}
}

View file

@ -24,6 +24,8 @@ import static google.registry.request.RequestParameters.extractSetOfParameters;
import com.google.appengine.api.taskqueue.Queue; import com.google.appengine.api.taskqueue.Queue;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.jcraft.jsch.SftpProgressMonitor;
import dagger.Binds;
import dagger.Module; import dagger.Module;
import dagger.Provides; import dagger.Provides;
import google.registry.request.Parameter; import google.registry.request.Parameter;
@ -38,7 +40,7 @@ import org.joda.time.DateTime;
* @see "google.registry.module.backend.BackendRequestComponent" * @see "google.registry.module.backend.BackendRequestComponent"
*/ */
@Module @Module
public final class RdeModule { public abstract class RdeModule {
public static final String PARAM_WATERMARK = "watermark"; public static final String PARAM_WATERMARK = "watermark";
public static final String PARAM_WATERMARKS = "watermarks"; public static final String PARAM_WATERMARKS = "watermarks";
@ -101,4 +103,10 @@ public final class RdeModule {
static Queue provideQueueRdeReport() { static Queue provideQueueRdeReport() {
return getQueue("rde-report"); return getQueue("rde-report");
} }
@Binds
abstract SftpProgressMonitor provideSftpProgressMonitor(
LoggingSftpProgressMonitor loggingSftpProgressMonitor);
private RdeModule() {}
} }

View file

@ -33,6 +33,7 @@ import com.google.common.flogger.FluentLogger;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException; import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpProgressMonitor;
import dagger.Lazy; import dagger.Lazy;
import google.registry.config.RegistryConfig.Config; import google.registry.config.RegistryConfig.Config;
import google.registry.gcs.GcsUtils; import google.registry.gcs.GcsUtils;
@ -108,6 +109,7 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
@Inject RydePgpFileOutputStreamFactory pgpFileFactory; @Inject RydePgpFileOutputStreamFactory pgpFileFactory;
@Inject RydePgpSigningOutputStreamFactory pgpSigningFactory; @Inject RydePgpSigningOutputStreamFactory pgpSigningFactory;
@Inject RydeTarOutputStreamFactory tarFactory; @Inject RydeTarOutputStreamFactory tarFactory;
@Inject SftpProgressMonitor sftpProgressMonitor;
@Inject TaskQueueUtils taskQueueUtils; @Inject TaskQueueUtils taskQueueUtils;
@Inject Retrier retrier; @Inject Retrier retrier;
@Inject @Parameter(RequestParameters.PARAM_TLD) String tld; @Inject @Parameter(RequestParameters.PARAM_TLD) String tld;
@ -202,8 +204,8 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
* }</pre> * }</pre>
*/ */
@VisibleForTesting @VisibleForTesting
protected void upload( protected void upload(GcsFilename xmlFile, long xmlLength, DateTime watermark, String name)
GcsFilename xmlFile, long xmlLength, DateTime watermark, String name) throws Exception { throws Exception {
logger.atInfo().log("Uploading XML file '%s' to remote path '%s'.", xmlFile, uploadUrl); logger.atInfo().log("Uploading XML file '%s' to remote path '%s'.", xmlFile, uploadUrl);
try (InputStream gcsInput = gcsUtils.openInputStream(xmlFile); try (InputStream gcsInput = gcsUtils.openInputStream(xmlFile);
Ghostryde.Decryptor decryptor = ghostryde.openDecryptor(gcsInput, stagingDecryptionKey); Ghostryde.Decryptor decryptor = ghostryde.openDecryptor(gcsInput, stagingDecryptionKey);
@ -214,7 +216,8 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
byte[] signature; byte[] signature;
String rydeFilename = name + ".ryde"; String rydeFilename = name + ".ryde";
GcsFilename rydeGcsFilename = new GcsFilename(bucket, rydeFilename); GcsFilename rydeGcsFilename = new GcsFilename(bucket, rydeFilename);
try (OutputStream ftpOutput = ftpChan.get().put(rydeFilename, OVERWRITE); try (OutputStream ftpOutput =
ftpChan.get().put(rydeFilename, sftpProgressMonitor, OVERWRITE);
OutputStream gcsOutput = gcsUtils.openOutputStream(rydeGcsFilename); OutputStream gcsOutput = gcsUtils.openOutputStream(rydeGcsFilename);
TeeOutputStream teeOutput = new TeeOutputStream(asList(ftpOutput, gcsOutput)); TeeOutputStream teeOutput = new TeeOutputStream(asList(ftpOutput, gcsOutput));
RydePgpSigningOutputStream signer = pgpSigningFactory.create(teeOutput, signingKey)) { RydePgpSigningOutputStream signer = pgpSigningFactory.create(teeOutput, signingKey)) {