aboutsummaryrefslogtreecommitdiff
path: root/streaming/src/test/java/org/apache
diff options
context:
space:
mode:
authorTathagata Das <tathagata.das1565@gmail.com>2015-04-29 13:06:11 -0700
committerTathagata Das <tathagata.das1565@gmail.com>2015-04-29 13:06:11 -0700
commit1868bd40dcce23990b98748b0239bd00452b1ca5 (patch)
treeb350c7f739a52e322f967194814aa433025ecbb4 /streaming/src/test/java/org/apache
parentc0c0ba6d2a11febc8e874c437c4f676dd36ae059 (diff)
downloadspark-1868bd40dcce23990b98748b0239bd00452b1ca5.tar.gz
spark-1868bd40dcce23990b98748b0239bd00452b1ca5.tar.bz2
spark-1868bd40dcce23990b98748b0239bd00452b1ca5.zip
[SPARK-7056] [STREAMING] Make the Write Ahead Log pluggable
Users may want the WAL data to be written to non-HDFS data storage systems. To allow that, we have to make the WAL pluggable. The following design doc outlines the plan. https://docs.google.com/a/databricks.com/document/d/1A2XaOLRFzvIZSi18i_luNw5Rmm9j2j4AigktXxIYxmY/edit?usp=sharing Things to add. * Unit tests for WriteAheadLogUtils Author: Tathagata Das <tathagata.das1565@gmail.com> Closes #5645 from tdas/wal-pluggable and squashes the following commits: 2c431fd [Tathagata Das] Minor fixes. c2bc7384 [Tathagata Das] More changes based on PR comments. 569a416 [Tathagata Das] fixed long line bde26b1 [Tathagata Das] Renamed segment to record handle everywhere b65e155 [Tathagata Das] More changes based on PR comments. d7cd15b [Tathagata Das] Fixed test 1a32a4b [Tathagata Das] Fixed test e0d19fb [Tathagata Das] Fixed defaults 9310cbf [Tathagata Das] style fix. 86abcb1 [Tathagata Das] Refactored WriteAheadLogUtils, and consolidated all WAL related configuration into it. 84ce469 [Tathagata Das] Added unit test and fixed compilation error. bce5e75 [Tathagata Das] Fixed long lines. 837c4f5 [Tathagata Das] Merge remote-tracking branch 'apache-github/master' into wal-pluggable 754fbf8 [Tathagata Das] Added license and docs. 09bc6fe [Tathagata Das] Merge remote-tracking branch 'apache-github/master' into wal-pluggable 7dd2d4b [Tathagata Das] Added pluggable WriteAheadLog interface, and refactored all code along with it
Diffstat (limited to 'streaming/src/test/java/org/apache')
-rw-r--r--streaming/src/test/java/org/apache/spark/streaming/JavaWriteAheadLogSuite.java129
1 files changed, 129 insertions, 0 deletions
diff --git a/streaming/src/test/java/org/apache/spark/streaming/JavaWriteAheadLogSuite.java b/streaming/src/test/java/org/apache/spark/streaming/JavaWriteAheadLogSuite.java
new file mode 100644
index 0000000000..50e8f9fc15
--- /dev/null
+++ b/streaming/src/test/java/org/apache/spark/streaming/JavaWriteAheadLogSuite.java
@@ -0,0 +1,129 @@
+/*
+ * 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.spark.streaming;
+
+import java.util.ArrayList;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.Transformer;
+import org.apache.spark.SparkConf;
+import org.apache.spark.streaming.util.WriteAheadLog;
+import org.apache.spark.streaming.util.WriteAheadLogRecordHandle;
+import org.apache.spark.streaming.util.WriteAheadLogUtils;
+
+import org.junit.Test;
+import org.junit.Assert;
+
+class JavaWriteAheadLogSuiteHandle extends WriteAheadLogRecordHandle {
+ int index = -1;
+ public JavaWriteAheadLogSuiteHandle(int idx) {
+ index = idx;
+ }
+}
+
+public class JavaWriteAheadLogSuite extends WriteAheadLog {
+
+ class Record {
+ long time;
+ int index;
+ ByteBuffer buffer;
+
+ public Record(long tym, int idx, ByteBuffer buf) {
+ index = idx;
+ time = tym;
+ buffer = buf;
+ }
+ }
+ private int index = -1;
+ private ArrayList<Record> records = new ArrayList<Record>();
+
+
+ // Methods for WriteAheadLog
+ @Override
+ public WriteAheadLogRecordHandle write(java.nio.ByteBuffer record, long time) {
+ index += 1;
+ records.add(new org.apache.spark.streaming.JavaWriteAheadLogSuite.Record(time, index, record));
+ return new JavaWriteAheadLogSuiteHandle(index);
+ }
+
+ @Override
+ public java.nio.ByteBuffer read(WriteAheadLogRecordHandle handle) {
+ if (handle instanceof JavaWriteAheadLogSuiteHandle) {
+ int reqdIndex = ((JavaWriteAheadLogSuiteHandle) handle).index;
+ for (Record record: records) {
+ if (record.index == reqdIndex) {
+ return record.buffer;
+ }
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public java.util.Iterator<java.nio.ByteBuffer> readAll() {
+ Collection<ByteBuffer> buffers = CollectionUtils.collect(records, new Transformer() {
+ @Override
+ public Object transform(Object input) {
+ return ((Record) input).buffer;
+ }
+ });
+ return buffers.iterator();
+ }
+
+ @Override
+ public void clean(long threshTime, boolean waitForCompletion) {
+ for (int i = 0; i < records.size(); i++) {
+ if (records.get(i).time < threshTime) {
+ records.remove(i);
+ i--;
+ }
+ }
+ }
+
+ @Override
+ public void close() {
+ records.clear();
+ }
+
+ @Test
+ public void testCustomWAL() {
+ SparkConf conf = new SparkConf();
+ conf.set("spark.streaming.driver.writeAheadLog.class", JavaWriteAheadLogSuite.class.getName());
+ WriteAheadLog wal = WriteAheadLogUtils.createLogForDriver(conf, null, null);
+
+ String data1 = "data1";
+ WriteAheadLogRecordHandle handle = wal.write(ByteBuffer.wrap(data1.getBytes()), 1234);
+ Assert.assertTrue(handle instanceof JavaWriteAheadLogSuiteHandle);
+ Assert.assertTrue(new String(wal.read(handle).array()).equals(data1));
+
+ wal.write(ByteBuffer.wrap("data2".getBytes()), 1235);
+ wal.write(ByteBuffer.wrap("data3".getBytes()), 1236);
+ wal.write(ByteBuffer.wrap("data4".getBytes()), 1237);
+ wal.clean(1236, false);
+
+ java.util.Iterator<java.nio.ByteBuffer> dataIterator = wal.readAll();
+ ArrayList<String> readData = new ArrayList<String>();
+ while (dataIterator.hasNext()) {
+ readData.add(new String(dataIterator.next().array()));
+ }
+ Assert.assertTrue(readData.equals(Arrays.asList("data3", "data4")));
+ }
+}