You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hugegraph.apache.org by "imbajin (via GitHub)" <gi...@apache.org> on 2024/02/01 10:21:09 UTC

Re: [PR] fix: concurrency issue causing file overwrite due to identical filenames [incubator-hugegraph-toolchain]

imbajin commented on code in PR #572:
URL: https://github.com/apache/incubator-hugegraph-toolchain/pull/572#discussion_r1474190566


##########
hugegraph-loader/src/main/java/org/apache/hugegraph/loader/direct/loader/HBaseDirectLoader.java:
##########
@@ -59,8 +62,69 @@ public class HBaseDirectLoader extends DirectLoader<ImmutableBytesWritable, KeyV
     private SinkToHBase sinkToHBase;
     private LoadDistributeMetrics loadDistributeMetrics;
 
+    private static final int RANDOM_VALUE1;
+    private static final short RANDOM_VALUE2;
+    private static final AtomicInteger NEXT_COUNTER;
+
     public static final Logger LOG = Log.logger(HBaseDirectLoader.class);
 
+    static {
+        try {
+            SecureRandom secureRandom = new SecureRandom();
+            RANDOM_VALUE1 = secureRandom.nextInt(0x01000000);
+            RANDOM_VALUE2 = (short) secureRandom.nextInt(0x00008000);
+            NEXT_COUNTER = new AtomicInteger(new SecureRandom().nextInt());
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    private static byte int3(final int x) {
+        return (byte) (x >> 24);
+    }
+
+    private static byte int2(final int x) {
+        return (byte) (x >> 16);
+    }
+
+    private static byte int1(final int x) {
+        return (byte) (x >> 8);
+    }
+
+    private static byte int0(final int x) {
+        return (byte) (x);
+    }
+
+    private static byte short1(final short x) {
+        return (byte) (x >> 8);
+    }
+
+    private static byte short0(final short x) {
+        return (byte) (x);
+    }
+
+    public static String fileID() {
+        long timeStamp = System.currentTimeMillis() / 1000;
+        ByteBuffer byteBuffer = ByteBuffer.allocate(12);
+
+        byteBuffer.put(int3((int) timeStamp));
+        byteBuffer.put(int2((int) timeStamp));
+        byteBuffer.put(int1((int) timeStamp));
+        byteBuffer.put(int0((int) timeStamp));
+
+        byteBuffer.put(int2(RANDOM_VALUE1));
+        byteBuffer.put(int1(RANDOM_VALUE1));
+        byteBuffer.put(int0(RANDOM_VALUE1));
+        byteBuffer.put(short1(RANDOM_VALUE2));
+        byteBuffer.put(short0(RANDOM_VALUE2));
+
+        byteBuffer.put(int2(NEXT_COUNTER.incrementAndGet()));
+        byteBuffer.put(int1(NEXT_COUNTER.incrementAndGet()));
+        byteBuffer.put(int0(NEXT_COUNTER.incrementAndGet()));
+
+        return Bytes.toHex(byteBuffer.array());
+    }

Review Comment:
   maybe we could leave/show a simple case for it? (hard to know the generated str now)



-- 
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.

To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@hugegraph.apache.org
For additional commands, e-mail: issues-help@hugegraph.apache.org