Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipe: export tsfile sink #14111

Open
wants to merge 1 commit into
base: dev/1.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.iotdb.commons.pipe.agent.plugin.constructor.PipeConnectorConstructor;
import org.apache.iotdb.commons.pipe.agent.plugin.meta.DataNodePipePluginMetaKeeper;
import org.apache.iotdb.db.pipe.connector.protocol.airgap.IoTDBDataRegionAirGapConnector;
import org.apache.iotdb.db.pipe.connector.protocol.exportfile.ExportTsFileConnector;
import org.apache.iotdb.db.pipe.connector.protocol.legacy.IoTDBLegacyPipeConnector;
import org.apache.iotdb.db.pipe.connector.protocol.opcua.OpcUaConnector;
import org.apache.iotdb.db.pipe.connector.protocol.pipeconsensus.PipeConsensusAsyncConnector;
Expand Down Expand Up @@ -69,6 +70,8 @@ protected void initConstructors() {
BuiltinPipePlugin.DO_NOTHING_CONNECTOR.getPipePluginName(), DoNothingConnector::new);
pluginConstructors.put(
BuiltinPipePlugin.WRITE_BACK_CONNECTOR.getPipePluginName(), WriteBackConnector::new);
pluginConstructors.put(
BuiltinPipePlugin.EXPORT_TSFILE_CONNECTOR.getPipePluginName(), ExportTsFileConnector::new);

pluginConstructors.put(
BuiltinPipePlugin.IOTDB_THRIFT_SINK.getPipePluginName(),
Expand Down Expand Up @@ -100,5 +103,7 @@ protected void initConstructors() {
pluginConstructors.put(
BuiltinPipePlugin.PIPE_CONSENSUS_ASYNC_SINK.getPipePluginName(),
PipeConsensusAsyncConnector::new);
pluginConstructors.put(
BuiltinPipePlugin.EXPORT_TSFILE_SINK.getPipePluginName(), ExportTsFileConnector::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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.iotdb.db.pipe.connector.protocol.exportfile;

import org.apache.iotdb.db.pipe.event.common.tsfile.PipeTsFileInsertionEvent;
import org.apache.iotdb.pipe.api.PipeConnector;
import org.apache.iotdb.pipe.api.customizer.configuration.PipeConnectorRuntimeConfiguration;
import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameterValidator;
import org.apache.iotdb.pipe.api.customizer.parameter.PipeParameters;
import org.apache.iotdb.pipe.api.event.Event;
import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent;
import org.apache.iotdb.pipe.api.event.dml.insertion.TsFileInsertionEvent;
import org.apache.iotdb.pipe.api.exception.PipeException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static org.apache.iotdb.commons.pipe.config.constant.PipeConnectorConstant.CONNECTOR_EXPORT_TSFILE_PATH_KEY;
import static org.apache.iotdb.commons.pipe.config.constant.PipeConnectorConstant.SINK_EXPORT_TSFILE_PATH_KEY;

public class ExportTsFileConnector implements PipeConnector {
private static final Logger LOGGER = LoggerFactory.getLogger(ExportTsFileConnector.class);

private File exportPath;

@Override
public void validate(PipeParameterValidator validator) throws Exception {
final PipeParameters parameters = validator.getParameters();
validator.validate(
args -> (boolean) args[0] || (boolean) args[1],
String.format(
"One of %s and %s must be specified",
CONNECTOR_EXPORT_TSFILE_PATH_KEY, SINK_EXPORT_TSFILE_PATH_KEY),
parameters.hasAttribute(CONNECTOR_EXPORT_TSFILE_PATH_KEY),
parameters.hasAttribute(SINK_EXPORT_TSFILE_PATH_KEY));
}

@Override
public void customize(PipeParameters parameters, PipeConnectorRuntimeConfiguration configuration)
throws Exception {
if (parameters.hasAttribute(SINK_EXPORT_TSFILE_PATH_KEY)) {
exportPath = new File(parameters.getString(SINK_EXPORT_TSFILE_PATH_KEY));
} else if (parameters.hasAttribute(CONNECTOR_EXPORT_TSFILE_PATH_KEY)) {
exportPath = new File(parameters.getString(CONNECTOR_EXPORT_TSFILE_PATH_KEY));
} else {
// This should not happen
throw new PipeException("Export TsFile path not found in parameters");
}
}

@Override
public void handshake() throws Exception {
if (exportPath == null) {
throw new PipeException("Handshake should not take place before customize");
}

if (!exportPath.exists() && !exportPath.mkdirs()) {
throw new PipeException("Export TsFile path not exist and can't be created");
}
Comment on lines +78 to +80
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!exportPath.exists() && !exportPath.mkdirs()) {
throw new PipeException("Export TsFile path not exist and can't be created");
}
synchornized (ExportTsFileConnector.class) {
if (!exportPath.exists() && !exportPath.mkdirs()) {
throw new PipeException("Export TsFile path not exist and can't be created");
}
}


if (!exportPath.isDirectory()) {
throw new PipeException("Export TsFile path already exists and is not a directory");
}
}

@Override
public void heartbeat() throws Exception {
handshake();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do nothing is better.

}

@Override
public void transfer(TabletInsertionEvent tabletInsertionEvent) throws Exception {
LOGGER.warn(
"TabletInsertionEvent is not supported, will skip this event: {}", tabletInsertionEvent);
}

@Override
public void transfer(TsFileInsertionEvent tsFileInsertionEvent) throws Exception {
if (!(tsFileInsertionEvent instanceof PipeTsFileInsertionEvent)) {
LOGGER.warn(
"{} only supports PipeTsFileInsertionEvent", ExportTsFileConnector.class.getName());
return;
}

final PipeTsFileInsertionEvent event = (PipeTsFileInsertionEvent) tsFileInsertionEvent;
final File sourceTsFile = event.getTsFile();
final File targetPath = new File(exportPath, sourceTsFile.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different regions, the same name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

客户目录检查下载文件的时候,会不会往子目录去检测?需要确认

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that sourceTsFile.getName() returns the name of pipe-pinned tsfiles, which include region id.


if (!event.increaseReferenceCount(ExportTsFileConnector.class.getName())) {
return;
}
try {
Files.copy(sourceTsFile.toPath(), targetPath.toPath());
} catch (final IOException e) {
LOGGER.warn(
"File renaming failed when exporting tsfile. Origin: {}, Target: {}",
sourceTsFile.getAbsolutePath(),
targetPath.getAbsolutePath(),
e);
} finally {
event.decreaseReferenceCount(ExportTsFileConnector.class.getName(), false);
}
}

@Override
public void transfer(Event event) throws Exception {
// Do nothing for generic events
}

@Override
public void close() throws Exception {
// Do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.commons.pipe.agent.plugin.builtin;

import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.donothing.DoNothingConnector;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.export.ExportTsFileConnector;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.iotdb.airgap.IoTDBAirGapConnector;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.iotdb.consensus.PipeConsensusAsyncConnector;
import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.iotdb.thrift.IoTDBLegacyPipeConnector;
Expand Down Expand Up @@ -87,6 +88,7 @@ public enum BuiltinPipePlugin {
WEBSOCKET_CONNECTOR("websocket-connector", WebSocketConnector.class),
OPC_UA_CONNECTOR("opc-ua-connector", OpcUaConnector.class),
WRITE_BACK_CONNECTOR("write-back-connector", WriteBackConnector.class),
EXPORT_TSFILE_CONNECTOR("export-tsfile-connector", ExportTsFileConnector.class),

DO_NOTHING_SINK("do-nothing-sink", DoNothingConnector.class),
IOTDB_THRIFT_SINK("iotdb-thrift-sink", IoTDBThriftConnector.class),
Expand All @@ -100,6 +102,7 @@ public enum BuiltinPipePlugin {
WRITE_BACK_SINK("write-back-sink", WriteBackConnector.class),
SUBSCRIPTION_SINK("subscription-sink", DoNothingConnector.class),
PIPE_CONSENSUS_ASYNC_SINK("pipe-consensus-async-sink", PipeConsensusAsyncConnector.class),
EXPORT_TSFILE_SINK("export-tsfile-sink", ExportTsFileConnector.class),
;

private final String pipePluginName;
Expand Down Expand Up @@ -155,6 +158,7 @@ public String getClassName() {
OPC_UA_CONNECTOR.getPipePluginName().toUpperCase(),
WRITE_BACK_CONNECTOR.getPipePluginName().toUpperCase(),
PIPE_CONSENSUS_ASYNC_CONNECTOR.getPipePluginName().toUpperCase(),
EXPORT_TSFILE_CONNECTOR.getPipePluginName().toUpperCase(),
// Sinks
IOTDB_THRIFT_SYNC_SINK.getPipePluginName().toUpperCase(),
IOTDB_THRIFT_ASYNC_SINK.getPipePluginName().toUpperCase(),
Expand All @@ -163,5 +167,6 @@ public String getClassName() {
OPC_UA_SINK.getPipePluginName().toUpperCase(),
WRITE_BACK_SINK.getPipePluginName().toUpperCase(),
SUBSCRIPTION_SINK.getPipePluginName().toUpperCase(),
PIPE_CONSENSUS_ASYNC_SINK.getPipePluginName().toUpperCase())));
PIPE_CONSENSUS_ASYNC_SINK.getPipePluginName().toUpperCase(),
EXPORT_TSFILE_SINK.getPipePluginName().toUpperCase())));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.iotdb.commons.pipe.agent.plugin.builtin.connector.export;

import org.apache.iotdb.commons.pipe.agent.plugin.builtin.connector.PlaceholderConnector;

/**
* This class is a placeholder and should not be initialized. It represents the Export TsFile
* connector. There is a real implementation in the server module but cannot be imported here. The
* pipe agent in the server module will replace this class with the real implementation when
* initializing the Export TsFile connector.
*/
public class ExportTsFileConnector extends PlaceholderConnector {}
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ public class PipeConnectorConstant {
CONNECTOR_LOAD_TSFILE_STRATEGY_ASYNC_VALUE,
CONNECTOR_LOAD_TSFILE_STRATEGY_SYNC_VALUE)));

public static final String CONNECTOR_EXPORT_TSFILE_PATH_KEY = "connector.export-tsfile.path";
public static final String SINK_EXPORT_TSFILE_PATH_KEY = "sink.export-tsfile.path";

private PipeConnectorConstant() {
throw new IllegalStateException("Utility class");
}
Expand Down
Loading