-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
BaseKeyValueStoreWriter.java
178 lines (159 loc) · 6.09 KB
/
BaseKeyValueStoreWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
* (the “License”). You may not use this work except in compliance with the License, which is
* available at www.apache.org/licenses/LICENSE-2.0
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied, as more fully set forth in the License.
*
* See the NOTICE file distributed with this work for information regarding copyright ownership.
*/
package alluxio.client.keyvalue;
import alluxio.AlluxioURI;
import alluxio.Constants;
import alluxio.client.ClientContext;
import alluxio.client.file.FileSystem;
import alluxio.exception.AlluxioException;
import alluxio.exception.ExceptionMessage;
import alluxio.exception.PreconditionMessage;
import alluxio.thrift.PartitionInfo;
import alluxio.util.io.BufferUtils;
import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;
import javax.annotation.concurrent.NotThreadSafe;
/**
* Default implementation of {@link KeyValueStoreWriter} to create an Alluxio key-value store.
*/
@NotThreadSafe
class BaseKeyValueStoreWriter implements KeyValueStoreWriter {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
private final FileSystem mFileSystem = FileSystem.Factory.get();
private final KeyValueMasterClient mMasterClient;
private final AlluxioURI mStoreUri;
private long mPartitionIndex;
private KeyValuePartitionWriter mWriter = null;
/** Min key in the current partition. */
private ByteBuffer mKeyStart = null;
/** Max key in the current partition. */
private ByteBuffer mKeyLimit = null;
/** Whether this writer is closed. */
private boolean mClosed;
/** Whether this writer is canceled. */
private boolean mCanceled;
/**
* Constructs a {@link BaseKeyValueStoreWriter}. This constructor will create a new key-value
* store at the given {@link AlluxioURI}.
*
* @param uri URI of the store
* @throws IOException if a non-Alluxio exception occurs
* @throws AlluxioException if an unexpected Alluxio exception is thrown
*/
BaseKeyValueStoreWriter(AlluxioURI uri) throws IOException, AlluxioException {
LOG.info("Create KeyValueStoreWriter for {}", uri);
mMasterClient =
new KeyValueMasterClient(ClientContext.getMasterAddress(), ClientContext.getConf());
mStoreUri = Preconditions.checkNotNull(uri);
mMasterClient.createStore(mStoreUri);
mPartitionIndex = 0;
mClosed = false;
}
@Override
public void close() throws IOException {
if (mClosed) {
return;
}
try {
if (mCanceled) {
mWriter.cancel();
// TODO(binfan): cancel all other written partitions
} else {
completePartition();
mMasterClient.completeStore(mStoreUri);
}
} catch (AlluxioException e) {
throw new IOException(e);
} finally {
mMasterClient.close();
}
mClosed = true;
}
@Override
public void cancel() throws IOException {
mCanceled = true;
close();
}
@Override
public void put(byte[] key, byte[] value) throws IOException, AlluxioException {
Preconditions.checkNotNull(key, PreconditionMessage.ERR_PUT_NULL_KEY);
Preconditions.checkNotNull(value, PreconditionMessage.ERR_PUT_NULL_KEY);
Preconditions.checkArgument(key.length > 0, PreconditionMessage.ERR_PUT_EMPTY_KEY);
Preconditions.checkArgument(value.length > 0, PreconditionMessage.ERR_PUT_EMPTY_VALUE);
// If this is the first put to the first partition in this store, create a new partition; or
// if this is a put to an existing but full partition, create a new partition and switch to
// this one.
if (mWriter == null || !mWriter.canPut(key, value)) {
// Need to save the existing partition before switching to the next partition.
if (mWriter != null) {
completePartition();
}
mWriter = KeyValuePartitionWriter.Factory.create(getPartitionName());
mKeyStart = null;
mKeyLimit = null;
}
// If we are still unable to put this key-value pair after switching partition, throw exception.
if (!mWriter.canPut(key, value)) {
throw new IOException(ExceptionMessage.KEY_VALUE_TOO_LARGE
.getMessage(key.length, value.length));
}
mWriter.put(key, value);
ByteBuffer keyBuf = ByteBuffer.wrap(key);
// Update the min key in the current partition.
if (mKeyStart == null || keyBuf.compareTo(mKeyStart) < 0) {
mKeyStart = ByteBuffer.allocate(key.length);
mKeyStart.put(key);
mKeyStart.flip();
}
// Update the max key in the current partition.
if (mKeyLimit == null || keyBuf.compareTo(mKeyLimit) > 0) {
mKeyLimit = ByteBuffer.allocate(key.length);
mKeyLimit.put(key);
mKeyLimit.flip();
}
}
@Override
public void put(ByteBuffer key, ByteBuffer value) throws IOException, AlluxioException {
Preconditions.checkNotNull(key, PreconditionMessage.ERR_PUT_NULL_KEY);
Preconditions.checkNotNull(value, PreconditionMessage.ERR_PUT_NULL_VALUE);
// TODO(binfan): make efficient implementation
byte[] keyArray = BufferUtils.newByteArrayFromByteBuffer(key);
byte[] valueArray = BufferUtils.newByteArrayFromByteBuffer(value);
put(keyArray, valueArray);
}
/**
* @return {@link AlluxioURI} to the current partition file
*/
private AlluxioURI getPartitionName() {
return new AlluxioURI(String.format("%s/part-%05d", mStoreUri, mPartitionIndex));
}
/**
* Completes the current partition.
*
* @throws IOException if non-Alluxio error occurs
* @throws AlluxioException if Alluxio error occurs
*/
private void completePartition() throws IOException, AlluxioException {
if (mWriter == null) {
return;
}
mWriter.close();
List<Long> blockIds = mFileSystem.getStatus(getPartitionName()).getBlockIds();
long blockId = blockIds.get(0);
PartitionInfo info = new PartitionInfo(mKeyStart, mKeyLimit, blockId);
mMasterClient.completePartition(mStoreUri, info);
mPartitionIndex++;
}
}