Skip to content

Commit b62c745

Browse files
zyxxooimbajin
andauthored
fix: concurrency issue causing file overwrite due to identical filenames (#572)
Co-authored-by: imbajin <[email protected]>
1 parent 60cca5e commit b62c745

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

hugegraph-loader/src/main/java/org/apache/hugegraph/loader/direct/loader/HBaseDirectLoader.java

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
package org.apache.hugegraph.loader.direct.loader;
1919

2020
import java.io.IOException;
21+
import java.nio.ByteBuffer;
22+
import java.security.SecureRandom;
2123
import java.util.Iterator;
2224
import java.util.LinkedList;
2325
import java.util.List;
2426

27+
import java.util.concurrent.atomic.AtomicInteger;
2528
import org.apache.hadoop.conf.Configuration;
2629
import org.apache.hadoop.fs.FileSystem;
2730
import org.apache.hadoop.fs.FsShell;
@@ -59,8 +62,69 @@ public class HBaseDirectLoader extends DirectLoader<ImmutableBytesWritable, KeyV
5962
private SinkToHBase sinkToHBase;
6063
private LoadDistributeMetrics loadDistributeMetrics;
6164

65+
private static final int RANDOM_VALUE1;
66+
private static final short RANDOM_VALUE2;
67+
private static final AtomicInteger NEXT_COUNTER;
68+
6269
public static final Logger LOG = Log.logger(HBaseDirectLoader.class);
6370

71+
static {
72+
try {
73+
SecureRandom secureRandom = new SecureRandom();
74+
RANDOM_VALUE1 = secureRandom.nextInt(0x01000000);
75+
RANDOM_VALUE2 = (short) secureRandom.nextInt(0x00008000);
76+
NEXT_COUNTER = new AtomicInteger(new SecureRandom().nextInt());
77+
} catch (Exception e) {
78+
throw new RuntimeException(e);
79+
}
80+
}
81+
82+
private static byte int3(final int x) {
83+
return (byte) (x >> 24);
84+
}
85+
86+
private static byte int2(final int x) {
87+
return (byte) (x >> 16);
88+
}
89+
90+
private static byte int1(final int x) {
91+
return (byte) (x >> 8);
92+
}
93+
94+
private static byte int0(final int x) {
95+
return (byte) (x);
96+
}
97+
98+
private static byte short1(final short x) {
99+
return (byte) (x >> 8);
100+
}
101+
102+
private static byte short0(final short x) {
103+
return (byte) (x);
104+
}
105+
106+
public static String fileID() {
107+
long timeStamp = System.currentTimeMillis() / 1000;
108+
ByteBuffer byteBuffer = ByteBuffer.allocate(12);
109+
110+
byteBuffer.put(int3((int) timeStamp));
111+
byteBuffer.put(int2((int) timeStamp));
112+
byteBuffer.put(int1((int) timeStamp));
113+
byteBuffer.put(int0((int) timeStamp));
114+
115+
byteBuffer.put(int2(RANDOM_VALUE1));
116+
byteBuffer.put(int1(RANDOM_VALUE1));
117+
byteBuffer.put(int0(RANDOM_VALUE1));
118+
byteBuffer.put(short1(RANDOM_VALUE2));
119+
byteBuffer.put(short0(RANDOM_VALUE2));
120+
121+
byteBuffer.put(int2(NEXT_COUNTER.incrementAndGet()));
122+
byteBuffer.put(int1(NEXT_COUNTER.incrementAndGet()));
123+
byteBuffer.put(int0(NEXT_COUNTER.incrementAndGet()));
124+
125+
return Bytes.toHex(byteBuffer.array());
126+
}
127+
64128
public HBaseDirectLoader(LoadOptions loadOptions,
65129
InputStruct struct,
66130
LoadDistributeMetrics loadDistributeMetrics) {
@@ -144,8 +208,8 @@ String generateFiles(JavaPairRDD<ImmutableBytesWritable, KeyValue> buildAndSerRd
144208

145209
public String getHFilePath(Configuration conf) throws IOException {
146210
FileSystem fs = FileSystem.get(conf);
147-
long timeStr = System.currentTimeMillis();
148-
String pathStr = fs.getWorkingDirectory().toString() + "/hfile-gen" + "/" + timeStr + "/";
211+
String fileID = fileID();
212+
String pathStr = fs.getWorkingDirectory().toString() + "/hfile-gen" + "/" + fileID + "/";
149213
Path hfileGenPath = new Path(pathStr);
150214
if (fs.exists(hfileGenPath)) {
151215
LOG.info("\n Delete the path where the hfile is generated,path {} ", pathStr);

0 commit comments

Comments
 (0)