TimePartitionedDataPublisher.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.wikimedia.gobblin.copy;

import java.io.IOException;

import org.apache.gobblin.configuration.State;
import org.apache.gobblin.configuration.WorkUnitState;
import org.apache.gobblin.util.FileListUtils;
import org.apache.gobblin.util.ParallelRunner;
import org.apache.gobblin.util.WriterUtils;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;


/**
 * For time partition jobs, writer output directory is
 * $GOBBLIN_WORK_DIR/task-output/{extractId}/{tableName}/{partitionPath},
 * where partition path is the time bucket, e.g., 2015/04/08/15.
 *
 * Publisher output directory is $GOBBLIN_WORK_DIR/job-output/{tableName}/{partitionPath}
 *
 * <p>
 * This is an updated copy of {@link org.apache.gobblin.publisher.TimePartitionedDataPublisher}
 * in gobblin-core module. This file should be deleted in favor of the upstream version when possible.
 * Updates are:
 * <ul>
 * <li>Override {@link #addWriterOutputToNewDir} to recursively iterate moving files from subfolders,
 * allowing to have detailed folders in states PUBLISH_DIRS -- https://github.com/apache/gobblin/pull/3409</li>
 * </ul>
 * </p>
 */
@SuppressFBWarnings(justification = "Class copied from upstream Gobblin")
public class TimePartitionedDataPublisher extends BaseDataPublisher {

    public TimePartitionedDataPublisher(State state) throws IOException {
        super(state);
    }

    /**
     * This method needs to be overridden for TimePartitionedDataPublisher, since the output folder structure
     * contains timestamp, we need to move the files recursively.
     *
     * For example, move {writerOutput}/2015/04/08/15/output.avro to {publisherOutput}/2015/04/08/15/output.avro
     */
    @Override
    protected void addWriterOutputToNewDir(Path writerOutput, Path publisherOutput, WorkUnitState workUnitState,
                                           int branchId, ParallelRunner parallelRunner) throws IOException {

        // Create the final output directory
        WriterUtils.mkdirsWithRecursivePermissionWithRetry(this.publisherFileSystemByBranches.get(branchId),
                publisherOutput, this.permissions.get(branchId), retrierConfig);
        // Now that the parent folder has been created, use addWriterOutputToExistingDir
        this.addWriterOutputToExistingDir(writerOutput, publisherOutput, workUnitState, branchId, parallelRunner);
    }

    /**
     * This method needs to be overridden for TimePartitionedDataPublisher, since the output folder structure
     * contains timestamp, we have to move the files recursively.
     *
     * For example, move {writerOutput}/2015/04/08/15/output.avro to {publisherOutput}/2015/04/08/15/output.avro
     */
    @Override
    protected void addWriterOutputToExistingDir(Path writerOutput, Path publisherOutput, WorkUnitState workUnitState,
                                                int branchId, ParallelRunner parallelRunner) throws IOException {

        for (FileStatus status : FileListUtils.listFilesRecursively(this.writerFileSystemByBranches.get(branchId), writerOutput)) {
            String filePathStr = status.getPath().toString();
            String pathSuffix = filePathStr.substring(filePathStr.indexOf(writerOutput.toString()) + writerOutput.toString().length() + 1);
            Path outputPath = new Path(publisherOutput, pathSuffix);

            WriterUtils.mkdirsWithRecursivePermissionWithRetry(this.publisherFileSystemByBranches.get(branchId), outputPath.getParent(),
                    this.permissions.get(branchId), this.retrierConfig);

            movePath(parallelRunner, workUnitState, status.getPath(), outputPath, branchId);
        }
    }
}