Skip to content

Commit 1dfc873

Browse files
committed
change blockid in pixels-cache from file name to real block id
1 parent 4cfd4dc commit 1dfc873

File tree

23 files changed

+343
-35
lines changed

23 files changed

+343
-35
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ package-info.java
2121
*.txt
2222
test30G_pixels/
2323
*.csv
24-
resources/*.xml
24+
resources/*.xml
25+
*.so
26+
.vscode

cpp/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all: JNITest
2+
3+
JNITest:
4+
$(CXX) -shared -fPIC JNITest.cc -o JNITest.so -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux/
5+
6+
clean:
7+
rm -rf ./JNITest.so
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cn.edu.ruc.iir.pixels.cache;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
6+
import java.util.Random;
7+
import java.util.concurrent.ConcurrentLinkedQueue;
8+
9+
/**
10+
* Created at: 19-5-12
11+
* Author: hank
12+
*/
13+
public class CacheLogger implements Runnable
14+
{
15+
private static final Logger logger = LogManager.getLogger(CacheLogger.class);
16+
17+
private ConcurrentLinkedQueue<Long> searchLatency = new ConcurrentLinkedQueue<>();
18+
private ConcurrentLinkedQueue<Long> readLatency = new ConcurrentLinkedQueue<>();
19+
private volatile boolean shutdown = false;
20+
21+
public void setShutdown()
22+
{
23+
this.shutdown = true;
24+
}
25+
26+
public void addSearchLatency(long latency)
27+
{
28+
this.searchLatency.add(latency);
29+
}
30+
31+
public void addReadLatency(long latency)
32+
{
33+
this.readLatency.add(latency);
34+
}
35+
36+
@Override
37+
public void run()
38+
{
39+
while (this.shutdown == false)
40+
{
41+
try
42+
{
43+
Thread.sleep(5000);
44+
} catch (InterruptedException e)
45+
{
46+
e.printStackTrace();
47+
}
48+
Object[] searches = searchLatency.toArray();
49+
searchLatency.clear();
50+
Object[] reads = readLatency.toArray();
51+
readLatency.clear();
52+
53+
long sum = 0;
54+
for (Object latency : searches)
55+
{
56+
sum += (Long) latency;
57+
}
58+
logger.info("avg search latency: (" + (sum*1.0/searches.length) + ") ns");
59+
60+
sum = 0;
61+
for (Object latency : reads)
62+
{
63+
sum += (Long) latency;
64+
}
65+
logger.info("avg read latency: (" + (sum*1.0/reads.length) + ") ns");
66+
67+
}
68+
}
69+
70+
public static void main(String[] args) throws InterruptedException
71+
{
72+
CacheLogger cacheLogger = new CacheLogger();
73+
Thread thread = new Thread(cacheLogger);
74+
thread.start();
75+
Random random = new Random();
76+
77+
for (int i = 0; i < 1000; ++i)
78+
{
79+
cacheLogger.addReadLatency(random.nextInt(100));
80+
cacheLogger.addSearchLatency(random.nextInt(200));
81+
Thread.sleep(100);
82+
}
83+
84+
cacheLogger.setShutdown();
85+
}
86+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.edu.ruc.iir.pixels.cache;
2+
3+
/**
4+
* Created at: 19-5-11
5+
* Author: hank
6+
*/
7+
public class NativePixelsCacheReader
8+
{
9+
private String indexFileLocation;
10+
private long indexFileSize;
11+
private String cacheFileLocation;
12+
private long cacheFileSize;
13+
14+
public NativePixelsCacheReader(String indexFileLocation, long indexFileSize,
15+
String cacheFileLocation, long cacheFileSize)
16+
{
17+
this.indexFileLocation = indexFileLocation;
18+
this.indexFileSize = indexFileSize;
19+
this.cacheFileLocation = cacheFileLocation;
20+
this.cacheFileSize = cacheFileSize;
21+
}
22+
23+
static
24+
{
25+
System.loadLibrary("lib_pixels.so");
26+
}
27+
28+
private native byte[] getFromCache();
29+
}

pixels-cache/src/main/java/cn/edu/ruc/iir/pixels/cache/PixelsCacheKey.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cn.edu.ruc.iir.pixels.cache;
22

33
import cn.edu.ruc.iir.pixels.common.utils.Constants;
4-
import org.apache.commons.compress.utils.CharsetNames;
54

65
import java.nio.ByteBuffer;
7-
import java.nio.charset.Charset;
86
import java.util.Arrays;
97
import java.util.Objects;
108

@@ -19,18 +17,18 @@ public class PixelsCacheKey
1917
{
2018
private final int SIZE = 2 * Short.BYTES + Constants.MAX_BLOCK_ID_LEN;
2119
private final ByteBuffer keyBuffer = ByteBuffer.allocate(SIZE);
22-
private String blockId; // TODO: it could be better to use long as the type of blockid
20+
private long blockId; // TODO: it could be better to use long as the type of blockid
2321
private short rowGroupId;
2422
private short columnId;
2523

26-
public PixelsCacheKey(String blockId, short rowGroupId, short columnId)
24+
public PixelsCacheKey(long blockId, short rowGroupId, short columnId)
2725
{
2826
this.blockId = blockId;
2927
this.rowGroupId = rowGroupId;
3028
this.columnId = columnId;
3129
}
3230

33-
public String getBlockId()
31+
public long getBlockId()
3432
{
3533
return blockId;
3634
}
@@ -53,7 +51,7 @@ public byte[] getBytes()
5351
// And we'd better use long (int64) for block id, instead of a string file name.
5452
// Fixed key length (12 bytes) should be more efficient. I noticed that block ids in hdfs-2.7.3 looks
5553
// like a sequence number, not really random.
56-
keyBuffer.put(blockId.getBytes(Charset.forName(CharsetNames.UTF_8)));
54+
keyBuffer.putLong(blockId);
5755
keyBuffer.putShort(rowGroupId);
5856
keyBuffer.putShort(columnId);
5957
return Arrays.copyOfRange(keyBuffer.array(), 0, keyBuffer.position());

pixels-cache/src/main/java/cn/edu/ruc/iir/pixels/cache/PixelsCacheReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static PixelsCacheReader.Builder newBuilder()
6666
* @param columnId column id
6767
* @return columnlet content
6868
* */
69-
public byte[] get(String blockId, short rowGroupId, short columnId)
69+
public byte[] get(long blockId, short rowGroupId, short columnId)
7070
{
7171
byte[] content = new byte[0];
7272
// check rw flag, if not readable, return empty bytes
@@ -110,7 +110,7 @@ public byte[] get(String blockId, short rowGroupId, short columnId)
110110
* This interface is only used by TESTS, DO NOT USE.
111111
* It will be removed soon!
112112
* */
113-
public PixelsCacheIdx search(String blockId, short rowGroupId, short columnId)
113+
public PixelsCacheIdx search(long blockId, short rowGroupId, short columnId)
114114
{
115115
PixelsCacheKey cacheKey = new PixelsCacheKey(blockId, rowGroupId, columnId);
116116
byte[] cacheKeyBytes = cacheKey.getBytes();

pixels-cache/src/main/java/cn/edu/ruc/iir/pixels/cache/PixelsCacheWriter.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.edu.ruc.iir.pixels.cache;
22

3+
import cn.edu.ruc.iir.pixels.common.exception.FSException;
34
import cn.edu.ruc.iir.pixels.common.metadata.domain.Compact;
45
import cn.edu.ruc.iir.pixels.common.metadata.domain.Layout;
56
import cn.edu.ruc.iir.pixels.common.utils.Constants;
@@ -172,6 +173,10 @@ public int updateAll(int version, Layout layout)
172173
catch (IOException e) {
173174
e.printStackTrace();
174175
return -1;
176+
} catch (FSException e)
177+
{
178+
e.printStackTrace();
179+
return -1;
175180
}
176181
}
177182

@@ -196,7 +201,7 @@ public void flush()
196201
}
197202

198203
private int internalUpdate(int version, Layout layout, String[] files)
199-
throws IOException
204+
throws IOException, FSException
200205
{
201206
int status = 0;
202207
// get the new caching layout
@@ -242,7 +247,7 @@ private int internalUpdate(int version, Layout layout, String[] files)
242247
break outer_loop;
243248
}
244249
else {
245-
radix.put(new PixelsCacheKey(file, rowGroupId, columnId), // TODO: it is not a good idea to use filename as block id.
250+
radix.put(new PixelsCacheKey(pixelsPhysicalReader.getCurrentBlockId(), rowGroupId, columnId), // TODO: it is not a good idea to use filename as block id.
246251
new PixelsCacheIdx(cacheOffset, physicalLens[i]));
247252
byte[] columnlet = pixelsPhysicalReader.read(physicalOffsets[i], physicalLens[i]);
248253
cacheFile.putBytes(cacheOffset, columnlet);

pixels-cache/src/main/java/cn/edu/ruc/iir/pixels/cache/PixelsPhysicalReader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.edu.ruc.iir.pixels.cache;
22

3+
import cn.edu.ruc.iir.pixels.common.exception.FSException;
34
import cn.edu.ruc.iir.pixels.common.physical.PhysicalReader;
45
import cn.edu.ruc.iir.pixels.common.physical.PhysicalReaderUtil;
56
import cn.edu.ruc.iir.pixels.core.PixelsProto;
@@ -38,6 +39,7 @@ private PixelsProto.FileTail readFileTail()
3839
return PixelsProto.FileTail.parseFrom(fileTailBuffer);
3940
}
4041
catch (IOException e) {
42+
// TODO: deal with this exception or throw it
4143
e.printStackTrace();
4244
}
4345
}
@@ -66,4 +68,9 @@ public byte[] read(long offset, int length)
6668

6769
return content;
6870
}
71+
72+
public long getCurrentBlockId() throws FSException
73+
{
74+
return physicalReader.getCurrentBlockId();
75+
}
6976
}

pixels-cache/src/test/c++/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
all: test_jni
2+
3+
test_jni:
4+
$(CXX) -shared -fPIC $@.cc -o $@.so -I/usr/lib/jvm/java-1.8.0-openjdk-amd64/include/ -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux/
5+
6+
clean:
7+
rm -rf ./TestJni.so
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <iostream>
2+
#include "test_jni.h"
3+
using namespace std;
4+
5+
static int sum = 0;
6+
7+
JNIEXPORT void JNICALL Java_cn_edu_ruc_iir_pixels_cache_TestJni_sayHello(JNIEnv *env, jclass cls)
8+
{
9+
cout << "Hello Native\n" << endl;
10+
}
11+
12+
JNIEXPORT jint JNICALL Java_cn_edu_ruc_iir_pixels_cache_TestJni_add
13+
(JNIEnv *env, jclass cls, jint a)
14+
{
15+
sum += a;
16+
return sum;
17+
}
18+
19+
JNIEXPORT jbyteArray JNICALL Java_cn_edu_ruc_iir_pixels_cache_TestJni_echo
20+
(JNIEnv *env, jclass cls, jbyteArray bytes, jint length)
21+
{
22+
jbyte* data = env->GetByteArrayElements(bytes, 0);
23+
jbyteArray res = env->NewByteArray(length);
24+
env->SetByteArrayRegion(res, 0, length, data);
25+
return res;
26+
}

0 commit comments

Comments
 (0)