Skip to content

Commit

Permalink
Merge pull request #2 from ydb-platform/small_improvments
Browse files Browse the repository at this point in the history
Small improvements
  • Loading branch information
zinal authored Sep 4, 2024
2 parents b4e8e77 + acd2da9 commit 9bcd751
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
40 changes: 39 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<version>1.7-SNAPSHOT</version>
<!-- <version>X.Y[-SNAPSHOT]</version> -->
<packaging>jar</packaging>

<name>YDB JDBC Importer</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -127,7 +130,42 @@
<scope>test</scope>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>jdk8-bootstrap</id>
<activation>
<jdk>[9</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release-profile</id>
<activation>
Expand Down Expand Up @@ -190,4 +228,4 @@
</build>
</profile>
</profiles>
</project>
</project>
49 changes: 27 additions & 22 deletions src/main/java/tech/ydb/importer/YdbImporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

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

import tech.ydb.importer.config.ImporterConfig;
import tech.ydb.importer.config.JdomHelper;
Expand All @@ -30,18 +37,12 @@
import tech.ydb.table.values.StructType;
import tech.ydb.table.values.Type;

import static tech.ydb.importer.config.JdomHelper.isBlank;

/**
*
* @author zinal
*/
public class YdbImporter {

private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(YdbImporter.class);

// X.Y[-SNAPSHOT]
public static final String VERSION = "1.7-SNAPSHOT";
private static final Logger LOG = LoggerFactory.getLogger(YdbImporter.class);

private final ImporterConfig config;
private final TableMapList tableMaps;
Expand Down Expand Up @@ -76,7 +77,7 @@ public AnyTableLister getTableLister() {

public void run() throws Exception {
String jdbcClassName = config.getSource().getClassName();
if (!isBlank(jdbcClassName)) {
if (!JdomHelper.isBlank(jdbcClassName)) {
LOG.info("Loading driver class {}", jdbcClassName);
Class.forName(config.getSource().getClassName());
}
Expand Down Expand Up @@ -138,8 +139,7 @@ public void run() throws Exception {
}

private ExecutorService makeWorkers() {
return Executors.newFixedThreadPool(config.getWorkers().getPoolSize(),
new WorkerFactory(this));
return Executors.newFixedThreadPool(config.getWorkers().getPoolSize(), new WorkerFactory());
}

private void retrieveSourceMetadata(List<TableDecision> tables, ExecutorService workers)
Expand Down Expand Up @@ -257,7 +257,7 @@ private void loadTableData(ExecutorService es, List<TableDecision> tables) throw
}

public static void main(String[] args) {
LOG.info("{} version {}", YdbImporter.class.getSimpleName(), VERSION);
LOG.info("{} version {}", YdbImporter.class.getSimpleName(), getVersion());
if (args.length != 1) {
LOG.info("Single argument is expected: config-file.xml");
System.exit(2);
Expand All @@ -279,26 +279,31 @@ public static void main(String[] args) {
}
}

public static final class WorkerFactory implements ThreadFactory {

private final YdbImporter owner;
private int counter = 0;

public WorkerFactory(YdbImporter owner) {
this.owner = owner;
public static String getVersion() {
try {
Properties prop = new Properties();
InputStream in = YdbImporter.class.getResourceAsStream("/importer_version.properties");
prop.load(in);
return prop.getProperty("version");
} catch (IOException ex) {
LOG.error("cannot load version", ex);
return "unknown";
}
}

public static final class WorkerFactory implements ThreadFactory {
private final AtomicInteger counter = new AtomicInteger();

@Override
public Thread newThread(Runnable r) {
int workerId = counter.getAndIncrement();
final Thread t = new Thread(() -> {
BlobSaver.initCounter(counter);
BlobSaver.initCounter(workerId);
r.run();
}, "YdbImporter-worker-" + counter);
}, "YdbImporter-worker-" + workerId);
t.setDaemon(false);
++counter;
return t;
}

}

}
1 change: 1 addition & 0 deletions src/main/resources/importer_version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version=${project.version}
4 changes: 2 additions & 2 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %21.21c{1}:%-4L %m%n"/>
</Console>
</Appenders>
<Loggers>
Expand All @@ -12,4 +12,4 @@
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
</Configuration>

0 comments on commit 9bcd751

Please sign in to comment.