fx19880617 commented on a change in pull request #6340: URL: https://github.com/apache/incubator-pinot/pull/6340#discussion_r545589449
########## File path: pinot-minion/src/main/java/org/apache/pinot/minion/executor/SegmentGenerationAndPushTaskExecutor.java ########## @@ -0,0 +1,256 @@ +/** + * 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.apache.pinot.minion.executor; + +import com.fasterxml.jackson.databind.JsonNode; +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import org.apache.commons.io.FileUtils; +import org.apache.pinot.common.utils.TarGzCompressionUtils; +import org.apache.pinot.core.minion.PinotTaskConfig; +import org.apache.pinot.plugin.ingestion.batch.common.SegmentGenerationTaskRunner; +import org.apache.pinot.plugin.ingestion.batch.common.SegmentGenerationUtils; +import org.apache.pinot.plugin.ingestion.batch.common.SegmentPushUtils; +import org.apache.pinot.spi.data.Schema; +import org.apache.pinot.spi.env.PinotConfiguration; +import org.apache.pinot.spi.filesystem.LocalPinotFS; +import org.apache.pinot.spi.filesystem.PinotFS; +import org.apache.pinot.spi.filesystem.PinotFSFactory; +import org.apache.pinot.spi.ingestion.batch.BatchConfigProperties; +import org.apache.pinot.spi.ingestion.batch.spec.Constants; +import org.apache.pinot.spi.ingestion.batch.spec.PinotClusterSpec; +import org.apache.pinot.spi.ingestion.batch.spec.PushJobSpec; +import org.apache.pinot.spi.ingestion.batch.spec.RecordReaderSpec; +import org.apache.pinot.spi.ingestion.batch.spec.SegmentGenerationJobSpec; +import org.apache.pinot.spi.ingestion.batch.spec.SegmentGenerationTaskSpec; +import org.apache.pinot.spi.ingestion.batch.spec.SegmentNameGeneratorSpec; +import org.apache.pinot.spi.ingestion.batch.spec.TableSpec; +import org.apache.pinot.spi.utils.DataSizeUtils; +import org.apache.pinot.spi.utils.IngestionConfigUtils; +import org.apache.pinot.spi.utils.JsonUtils; +import org.apache.pinot.spi.utils.retry.AttemptsExceededException; +import org.apache.pinot.spi.utils.retry.RetriableOperationException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class SegmentGenerationAndPushTaskExecutor extends BaseTaskExecutor { + private static final Logger LOGGER = LoggerFactory.getLogger(SegmentGenerationAndPushTaskExecutor.class); + + private static final PinotFS LOCAL_PINOT_FS = new LocalPinotFS(); + private static final int DEFUALT_PUSH_ATTEMPTS = 5; + private static final int DEFAULT_PUSH_PARALLELISM = 1; + private static final long DEFAULT_PUSH_RETRY_INTERVAL_MILLIS = 1000L; + + @Override + public Object executeTask(PinotTaskConfig pinotTaskConfig) { + Map<String, String> taskConfigs = pinotTaskConfig.getConfigs(); + SegmentGenerationAndPushResult.Builder resultBuilder = new SegmentGenerationAndPushResult.Builder(); + File localTempDir = new File(FileUtils.getTempDirectory(), "pinot-" + UUID.randomUUID()); + + try { + // Generate Pinot Segment + SegmentGenerationTaskSpec taskSpec = generateTaskSpec(taskConfigs, localTempDir); + SegmentGenerationTaskRunner taskRunner = new SegmentGenerationTaskRunner(taskSpec); + String segmentName = taskRunner.run(); + + // Tar segment directory to compress file + File localSegmentTarFile = tarSegmentDir(taskSpec, segmentName); + + //move segment to output PinotFS + URI outputSegmentTarURI = moveSegmentToOutputPinotFS(taskConfigs, localSegmentTarFile); + + resultBuilder.setSegmentName(segmentName); + // Segment push task + pushSegment(taskSpec.getTableConfig().get(BatchConfigProperties.TABLE_NAME).asText(), taskConfigs, + outputSegmentTarURI); + resultBuilder.setSucceed(true); + } catch (Exception e) { + resultBuilder.setException(e); + } finally { + // Cleanup output dir + FileUtils.deleteQuietly(localTempDir); + } + return resultBuilder.build(); + } + + private void pushSegment(String tableName, Map<String, String> taskConfigs, URI outputSegmentTarURI) + throws Exception { + String pushMode = taskConfigs.get(BatchConfigProperties.PUSH_MODE); + + PushJobSpec pushJobSpec = new PushJobSpec(); + pushJobSpec.setPushAttempts(DEFUALT_PUSH_ATTEMPTS); + pushJobSpec.setPushParallelism(DEFAULT_PUSH_PARALLELISM); + pushJobSpec.setPushRetryIntervalMillis(DEFAULT_PUSH_RETRY_INTERVAL_MILLIS); + pushJobSpec.setSegmentUriPrefix(taskConfigs.get(BatchConfigProperties.PUSH_SEGMENT_URI_PREFIX)); + pushJobSpec.setSegmentUriSuffix(taskConfigs.get(BatchConfigProperties.PUSH_SEGMENT_URI_SUFFIX)); + + SegmentGenerationJobSpec spec = generatePushJobSpec(tableName, taskConfigs, pushJobSpec); + + URI outputSegmentDirURI = URI.create(taskConfigs.get(BatchConfigProperties.OUTPUT_SEGMENT_DIR_URI)); + PinotFS outputFileFS = getPinotFS(outputSegmentDirURI); + switch (BatchConfigProperties.SegmentPushType.valueOf(pushMode.toUpperCase())) { + case TAR: + try { + SegmentPushUtils.pushSegments(spec, LOCAL_PINOT_FS, Arrays.asList(outputSegmentTarURI.toString())); + } catch (RetriableOperationException | AttemptsExceededException e) { + throw new RuntimeException(e); + } + break; + case URI: + try { + List<String> segmentUris = new ArrayList<>(); + URI updatedURI = SegmentPushUtils + .generateSegmentTarURI(outputSegmentDirURI, outputSegmentTarURI, pushJobSpec.getSegmentUriPrefix(), + pushJobSpec.getSegmentUriSuffix()); + segmentUris.add(updatedURI.toString()); + SegmentPushUtils.sendSegmentUris(spec, segmentUris); + } catch (RetriableOperationException | AttemptsExceededException e) { + throw new RuntimeException(e); + } + break; + case METADATA: + try { + Map<String, String> segmentUriToTarPathMap = SegmentPushUtils + .getSegmentUriToTarPathMap(outputSegmentDirURI, pushJobSpec.getSegmentUriPrefix(), + pushJobSpec.getSegmentUriSuffix(), new String[]{outputSegmentTarURI.toString()}); + SegmentPushUtils.sendSegmentUriAndMetadata(spec, outputFileFS, segmentUriToTarPathMap); + } catch (RetriableOperationException | AttemptsExceededException e) { + throw new RuntimeException(e); + } + break; + default: + throw new UnsupportedOperationException("Unrecognized push mode - " + pushMode); + } + } + + private SegmentGenerationJobSpec generatePushJobSpec(String tableName, Map<String, String> taskConfigs, + PushJobSpec pushJobSpec) { + + TableSpec tableSpec = new TableSpec(); + tableSpec.setTableName(tableName); + + PinotClusterSpec pinotClusterSpec = new PinotClusterSpec(); + pinotClusterSpec.setControllerURI(taskConfigs.get(BatchConfigProperties.PUSH_CONTROLLER_URI)); + PinotClusterSpec[] pinotClusterSpecs = new PinotClusterSpec[]{pinotClusterSpec}; + + SegmentGenerationJobSpec spec = new SegmentGenerationJobSpec(); + spec.setPushJobSpec(pushJobSpec); + spec.setTableSpec(tableSpec); + spec.setPinotClusterSpecs(pinotClusterSpecs); + return spec; + } + + private URI moveSegmentToOutputPinotFS(Map<String, String> taskConfigs, File localSegmentTarFile) + throws Exception { + URI outputSegmentDirURI = URI.create(taskConfigs.get(BatchConfigProperties.OUTPUT_SEGMENT_DIR_URI)); + PinotFS outputFileFS = getPinotFS(outputSegmentDirURI); Review comment: fixed ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org