Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.ramcloud; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.File; |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
| 5 | import java.nio.ByteOrder; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Arrays; |
| 8 | import java.util.Collection; |
| 9 | import java.util.Iterator; |
| 10 | import java.util.List; |
| 11 | import java.util.concurrent.ConcurrentHashMap; |
| 12 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.datastore.IKVClient; |
| 14 | import net.onrc.onos.core.datastore.IKVTable; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 15 | import net.onrc.onos.core.datastore.IKVTable.IKVEntry; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 16 | import net.onrc.onos.core.datastore.IKVTableID; |
| 17 | import net.onrc.onos.core.datastore.IMultiEntryOperation; |
Jonathan Hart | a99ec67 | 2014-04-03 11:30:34 -0700 | [diff] [blame] | 18 | import net.onrc.onos.core.datastore.IMultiEntryOperation.STATUS; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 19 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 20 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 21 | import net.onrc.onos.core.datastore.WrongVersionException; |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 22 | import net.onrc.onos.core.datastore.internal.IModifiableMultiEntryOperation; |
| 23 | import net.onrc.onos.core.datastore.ramcloud.RCTable.Entry; |
| 24 | import net.onrc.onos.core.datastore.utils.ByteArrayUtil; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 25 | |
| 26 | import org.apache.commons.configuration.Configuration; |
| 27 | import org.apache.commons.configuration.ConfigurationException; |
| 28 | import org.apache.commons.configuration.PropertiesConfiguration; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import edu.stanford.ramcloud.JRamCloud; |
| 33 | import edu.stanford.ramcloud.JRamCloud.MultiReadObject; |
| 34 | import edu.stanford.ramcloud.JRamCloud.MultiWriteObject; |
| 35 | import edu.stanford.ramcloud.JRamCloud.MultiWriteRspObject; |
| 36 | import edu.stanford.ramcloud.JRamCloud.RejectRules; |
| 37 | import edu.stanford.ramcloud.JRamCloud.RejectRulesException; |
| 38 | import edu.stanford.ramcloud.JRamCloud.TableEnumerator2; |
| 39 | |
| 40 | public class RCClient implements IKVClient { |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 42 | private static final Logger log = LoggerFactory.getLogger(RCClient.class); |
| 43 | |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 44 | private static final String DEFAULT_LOCATOR = "zk:localhost:2181"; |
| 45 | private static final String DEFAULT_CLUSTERNAME = "ONOS-RC"; |
| 46 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 47 | private static final String DB_CONFIG_FILE = "conf/ramcloud.conf"; |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 48 | public static final Configuration CONFIG = getConfiguration(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 49 | |
| 50 | // Value taken from RAMCloud's Status.h |
| 51 | // FIXME These constants should be defined by JRamCloud |
| 52 | public static final int STATUS_OK = 0; |
| 53 | |
Yuta HIGUCHI | e316d5c | 2014-04-19 13:37:38 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Maximum number of Multi-Read operations which can be executed in |
| 56 | * one RPC call. |
| 57 | * |
| 58 | * There are multiple factors which determines this limit. |
| 59 | * - RAMCloud RPC size limit of 8MB. |
| 60 | * - JNI implementation store the RPC result on stack. |
| 61 | * (Increasing the stack-size limit will help relaxing this limit.) |
| 62 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 63 | public static final int MAX_MULTI_READS = Math.max(1, Integer |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 64 | .valueOf(System.getProperty("ramcloud.max_multi_reads", "400"))); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 65 | |
Yuta HIGUCHI | e316d5c | 2014-04-19 13:37:38 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Maximum number of Multi-Write operations which can be executed in |
| 68 | * one RPC call. |
| 69 | * |
| 70 | * There are multiple factors which determines this limit. |
| 71 | * - RAMCloud RPC size limit of 8MB. |
| 72 | * - JNI implementation store the RPC result on stack. |
| 73 | * (Increasing the stack-size limit will help relaxing this limit.) |
| 74 | */ |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 75 | public static final int MAX_MULTI_WRITES = Math.max(1, Integer |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 76 | .valueOf(System.getProperty("ramcloud.max_multi_writes", "800"))); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 77 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 78 | private static final ThreadLocal<JRamCloud> TLS_RC_CLIENT = new ThreadLocal<JRamCloud>() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 79 | @Override |
| 80 | protected JRamCloud initialValue() { |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 81 | return new JRamCloud(getLocator(CONFIG), getClusterName(CONFIG)); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 82 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * @return JRamCloud instance intended to be used only within the |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | * SameThread. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 88 | * @note Do not store the returned instance in a member variable, etc. which |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | * may be accessed later by another thread. |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 90 | */ |
| 91 | static JRamCloud getJRamCloudClient() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 92 | return TLS_RC_CLIENT.get(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // Currently RCClient is state-less |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 96 | private static final RCClient THE_INSTANCE = new RCClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 97 | |
| 98 | public static RCClient getClient() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 99 | return THE_INSTANCE; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public static final Configuration getConfiguration() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 103 | final File configFile = new File(System.getProperty("ramcloud.config.path", DB_CONFIG_FILE)); |
| 104 | return getConfiguration(configFile); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | public static final Configuration getConfiguration(final File configFile) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 108 | if (configFile == null) { |
| 109 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 110 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 111 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 112 | if (!configFile.isFile()) { |
| 113 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 114 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 115 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 116 | try { |
| 117 | return new PropertiesConfiguration(configFile); |
| 118 | } catch (ConfigurationException e) { |
| 119 | throw new IllegalArgumentException("Could not load configuration at: " + configFile, e); |
| 120 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 123 | public static String getLocator(final Configuration configuration) { |
| 124 | |
| 125 | final String locator = configuration.getString("ramcloud.locator"); |
| 126 | if (locator != null) { |
| 127 | return locator; |
| 128 | } |
| 129 | |
| 130 | // TODO Stop reading obsolete coordinatorIp, etc. once we're ready. |
| 131 | final String coordinatorIp = configuration.getString("ramcloud.coordinatorIp"); |
| 132 | if (coordinatorIp == null) { |
| 133 | return DEFAULT_LOCATOR; |
| 134 | } |
| 135 | |
| 136 | final String coordinatorPort = configuration.getString("ramcloud.coordinatorPort"); |
| 137 | if (coordinatorPort == null) { |
| 138 | return DEFAULT_LOCATOR; |
| 139 | } |
| 140 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 141 | final String coordinatorURL = coordinatorIp + "," + coordinatorPort; |
| 142 | return coordinatorURL; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Yuta HIGUCHI | d150ece | 2014-04-29 16:25:36 -0700 | [diff] [blame] | 145 | public static String getClusterName(final Configuration configuration) { |
| 146 | final String clusterName = configuration.getString("ramcloud.clusterName"); |
| 147 | if (clusterName != null) { |
| 148 | return clusterName; |
| 149 | } |
| 150 | |
| 151 | return DEFAULT_CLUSTERNAME; |
| 152 | } |
| 153 | |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public IMultiEntryOperation createOp(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 156 | return RCMultiEntryOperation.create(tableId, key, value); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param tableId RCTableID instance |
| 161 | */ |
| 162 | @Override |
| 163 | public long create(IKVTableID tableId, byte[] key, byte[] value) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 164 | throws ObjectExistsException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 165 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 166 | RCTableID rcTableId = (RCTableID) tableId; |
| 167 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 168 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 169 | RejectRules rules = new RejectRules(); |
| 170 | rules.rejectIfExists(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 171 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 172 | try { |
| 173 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 174 | } catch (JRamCloud.ObjectExistsException e) { |
| 175 | throw new ObjectExistsException(rcTableId, key, e); |
| 176 | } catch (JRamCloud.RejectRulesException e) { |
| 177 | log.error("Unexpected RejectRulesException", e); |
| 178 | return JRamCloud.VERSION_NONEXISTENT; |
| 179 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public IMultiEntryOperation forceCreateOp(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 184 | return RCMultiEntryOperation.forceCreate(tableId, key, value); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public long forceCreate(IKVTableID tableId, byte[] key, byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 189 | RCTableID rcTableId = (RCTableID) tableId; |
| 190 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 191 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 192 | long updatedVersion = rcClient.write(rcTableId.getTableID(), key, value); |
| 193 | return updatedVersion; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public IMultiEntryOperation readOp(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 198 | return RCMultiEntryOperation.read(tableId, key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public IKVEntry read(IKVTableID tableId, byte[] key) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 203 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 204 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 205 | RCTableID rcTableId = (RCTableID) tableId; |
| 206 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 207 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 208 | RejectRules rules = new RejectRules(); |
| 209 | rules.rejectIfDoesntExists(); |
| 210 | try { |
| 211 | JRamCloud.Object rcObj = rcClient.read(rcTableId.getTableID(), key, rules); |
| 212 | return new Entry(rcObj.key, rcObj.value, rcObj.version); |
| 213 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 214 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 215 | } catch (JRamCloud.RejectRulesException e) { |
| 216 | log.error("Unexpected RejectRulesException", e); |
| 217 | return null; |
| 218 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 222 | public IMultiEntryOperation updateOp(IKVTableID tableId, byte[] key, byte[] value, long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 223 | return RCMultiEntryOperation.update(tableId, key, value, version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public long update(IKVTableID tableId, byte[] key, byte[] value, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 228 | long version) throws ObjectDoesntExistException, |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 229 | WrongVersionException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 230 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 231 | RCTableID rcTableId = (RCTableID) tableId; |
| 232 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 233 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 234 | RejectRules rules = new RejectRules(); |
| 235 | rules.rejectIfDoesntExists(); |
| 236 | rules.rejectIfNeVersion(version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 237 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 238 | try { |
| 239 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 240 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 241 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 242 | } catch (JRamCloud.WrongVersionException e) { |
| 243 | throw new WrongVersionException(rcTableId, key, version, e); |
| 244 | } catch (JRamCloud.RejectRulesException e) { |
| 245 | log.error("Unexpected RejectRulesException", e); |
| 246 | return JRamCloud.VERSION_NONEXISTENT; |
| 247 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
| 251 | @Override |
| 252 | public long update(IKVTableID tableId, byte[] key, byte[] value) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 253 | throws ObjectDoesntExistException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 254 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 255 | RCTableID rcTableId = (RCTableID) tableId; |
| 256 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 257 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 258 | RejectRules rules = new RejectRules(); |
| 259 | rules.rejectIfDoesntExists(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 260 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 261 | try { |
| 262 | return rcClient.write(rcTableId.getTableID(), key, value, rules); |
| 263 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 264 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 265 | } catch (JRamCloud.RejectRulesException e) { |
| 266 | log.error("Unexpected RejectRulesException", e); |
| 267 | return JRamCloud.VERSION_NONEXISTENT; |
| 268 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 272 | public IMultiEntryOperation deleteOp(IKVTableID tableId, byte[] key, byte[] value, long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 273 | return RCMultiEntryOperation.delete(tableId, key, value, version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public long delete(IKVTableID tableId, byte[] key, long version) |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 278 | throws ObjectDoesntExistException, WrongVersionException { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 279 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 280 | RCTableID rcTableId = (RCTableID) tableId; |
| 281 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 282 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 283 | RejectRules rules = new RejectRules(); |
| 284 | rules.rejectIfDoesntExists(); |
| 285 | rules.rejectIfNeVersion(version); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 286 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 287 | try { |
| 288 | return rcClient.remove(rcTableId.getTableID(), key, rules); |
| 289 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 290 | throw new ObjectDoesntExistException(rcTableId, key, e); |
| 291 | } catch (JRamCloud.WrongVersionException e) { |
| 292 | throw new WrongVersionException(rcTableId, key, version, e); |
| 293 | } catch (JRamCloud.RejectRulesException e) { |
| 294 | log.error("Unexpected RejectRulesException", e); |
| 295 | return JRamCloud.VERSION_NONEXISTENT; |
| 296 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public IMultiEntryOperation forceDeleteOp(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 301 | return RCMultiEntryOperation.forceDelete(tableId, key); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public long forceDelete(IKVTableID tableId, byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 306 | RCTableID rcTableId = (RCTableID) tableId; |
| 307 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 308 | final long removedVersion = rcClient.remove(rcTableId.getTableID(), key); |
| 309 | return removedVersion; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public Iterable<IKVEntry> getAllEntries(IKVTableID tableId) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 314 | return new RCTableEntryIterable((RCTableID) tableId); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static class RCTableEntryIterable implements Iterable<IKVEntry> { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 318 | private final RCTableID tableId; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 319 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 320 | public RCTableEntryIterable(final RCTableID tableId) { |
| 321 | this.tableId = tableId; |
| 322 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 323 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 324 | @Override |
| 325 | public Iterator<IKVEntry> iterator() { |
| 326 | return new RCClient.RCTableIterator(tableId); |
| 327 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | public static class RCTableIterator implements Iterator<IKVEntry> { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 331 | private final RCTableID tableId; |
| 332 | protected final TableEnumerator2 enumerator; |
| 333 | private JRamCloud.Object last; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 334 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 335 | public RCTableIterator(final RCTableID tableId) { |
| 336 | this.tableId = tableId; |
| 337 | this.enumerator = getJRamCloudClient().new TableEnumerator2(tableId.getTableID()); |
| 338 | this.last = null; |
| 339 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 340 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 341 | @Override |
| 342 | public boolean hasNext() { |
| 343 | return this.enumerator.hasNext(); |
| 344 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 345 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 346 | @Override |
| 347 | public RCTable.Entry next() { |
| 348 | last = enumerator.next(); |
| 349 | return new RCTable.Entry(last.key, last.value, last.version); |
| 350 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 351 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 352 | @Override |
| 353 | public void remove() { |
| 354 | if (last != null) { |
| 355 | getJRamCloudClient(); |
| 356 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 357 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 358 | RejectRules rules = new RejectRules(); |
| 359 | rules.rejectIfNeVersion(last.version); |
| 360 | try { |
| 361 | rcClient.remove(tableId.getTableID(), last.key, rules); |
| 362 | } catch (RejectRulesException e) { |
| 363 | log.trace("remove failed", e); |
| 364 | } |
| 365 | last = null; |
| 366 | } |
| 367 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public boolean multiRead(final Collection<IMultiEntryOperation> ops) { |
| 372 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 373 | if (ops.size() <= MAX_MULTI_READS && ops instanceof ArrayList) { |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 374 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 375 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList) ops; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 376 | return multiReadInternal(arrays); |
| 377 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 378 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 379 | boolean failExists = false; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 380 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 381 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 382 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 383 | while (it.hasNext()) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 384 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 385 | req.add((RCMultiEntryOperation) it.next()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 386 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 387 | if (req.size() >= MAX_MULTI_READS) { |
| 388 | // dispatch multiRead |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 389 | failExists |= multiReadInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 390 | req.clear(); |
| 391 | } |
| 392 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 393 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 394 | if (!req.isEmpty()) { |
| 395 | // dispatch multiRead |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 396 | failExists |= multiReadInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 397 | req.clear(); |
| 398 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 399 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 400 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public boolean multiWrite(final List<IMultiEntryOperation> ops) { |
| 405 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 406 | if (ops.size() <= MAX_MULTI_WRITES && ops instanceof ArrayList) { |
Ray Milkey | 7f1567c | 2014-04-08 13:53:32 -0700 | [diff] [blame] | 407 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 408 | final ArrayList<RCMultiEntryOperation> arrays = (ArrayList) ops; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 409 | return multiWriteInternal(arrays); |
| 410 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 411 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 412 | boolean failExists = false; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 413 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 414 | ArrayList<RCMultiEntryOperation> req = new ArrayList<>(); |
| 415 | Iterator<IMultiEntryOperation> it = ops.iterator(); |
| 416 | while (it.hasNext()) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 417 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 418 | req.add((RCMultiEntryOperation) it.next()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 419 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 420 | if (req.size() >= MAX_MULTI_WRITES) { |
| 421 | // dispatch multiWrite |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 422 | failExists |= multiWriteInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 423 | req.clear(); |
| 424 | } |
| 425 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 426 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 427 | if (!req.isEmpty()) { |
| 428 | // dispatch multiWrite |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 429 | failExists |= multiWriteInternal(req); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 430 | req.clear(); |
| 431 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 432 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 433 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public boolean multiDelete(final Collection<IMultiEntryOperation> ops) { |
| 438 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 439 | // TODO implement multiRemove JNI, etc. if we need performance |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 440 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 441 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 442 | JRamCloud rcClient = getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 443 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 444 | for (IMultiEntryOperation iop : ops) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 445 | RCMultiEntryOperation op = (RCMultiEntryOperation) iop; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 446 | switch (op.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 447 | case DELETE: |
| 448 | RejectRules rules = new RejectRules(); |
| 449 | rules.rejectIfDoesntExists(); |
| 450 | rules.rejectIfNeVersion(op.getVersion()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 451 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 452 | try { |
| 453 | final long removedVersion = rcClient.remove(op.tableId.getTableID(), op.entry.getKey(), rules); |
| 454 | op.entry.setVersion(removedVersion); |
| 455 | op.status = STATUS.SUCCESS; |
| 456 | } catch (JRamCloud.ObjectDoesntExistException | JRamCloud.WrongVersionException e) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 457 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), "") + " from tableID:" + op.tableId, e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 458 | failExists = true; |
| 459 | op.status = STATUS.FAILED; |
| 460 | } catch (JRamCloud.RejectRulesException e) { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 461 | log.error("Failed to remove key:" + ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), "") + " from tableID:" + op.tableId, e); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 462 | failExists = true; |
| 463 | op.status = STATUS.FAILED; |
| 464 | } |
| 465 | break; |
| 466 | |
| 467 | case FORCE_DELETE: |
| 468 | final long removedVersion = rcClient.remove(op.tableId.getTableID(), op.entry.getKey()); |
| 469 | if (removedVersion != VERSION_NONEXISTENT) { |
| 470 | op.entry.setVersion(removedVersion); |
| 471 | op.status = STATUS.SUCCESS; |
| 472 | } else { |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 473 | log.error("Failed to remove key:{} from tableID:{}", ByteArrayUtil.toHexStringBuilder(op.entry.getKey(), ""), op.tableId); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 474 | failExists = true; |
| 475 | op.status = STATUS.FAILED; |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | default: |
| 480 | log.error("Invalid operation {} specified on multiDelete", op.getOperation()); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 481 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 482 | op.status = STATUS.FAILED; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 483 | break; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 486 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | private boolean multiReadInternal(final ArrayList<RCMultiEntryOperation> ops) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 490 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 491 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 492 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 493 | final int reqs = ops.size(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 494 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 495 | MultiReadObject multiReadObjects = new MultiReadObject(reqs); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 496 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 497 | // setup multi-read operation objects |
| 498 | for (int i = 0; i < reqs; ++i) { |
| 499 | IMultiEntryOperation op = ops.get(i); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 500 | multiReadObjects.setObject(i, ((RCTableID) op.getTableId()).getTableID(), op.getKey()); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 501 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 502 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 503 | // execute |
| 504 | JRamCloud.Object[] results = rcClient.multiRead(multiReadObjects.tableId, multiReadObjects.key, multiReadObjects.keyLength, reqs); |
| 505 | if (results.length != reqs) { |
| 506 | log.error("multiRead returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 507 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 508 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 509 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 510 | for (int i = 0; i < results.length; ++i) { |
| 511 | IModifiableMultiEntryOperation op = ops.get(i); |
| 512 | if (results[i] == null) { |
Yuta HIGUCHI | f148aac | 2014-05-05 14:59:06 -0700 | [diff] [blame] | 513 | // Logging as error gets too noisy when doing speculative read. |
| 514 | log.trace("MultiRead error {}, {}", op.getTableId(), op); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 515 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 516 | op.setStatus(STATUS.FAILED); |
| 517 | continue; |
| 518 | } |
| 519 | assert (Arrays.equals(results[i].key, op.getKey())); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 520 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 521 | op.setValue(results[i].value, results[i].version); |
| 522 | if (results[i].version == JRamCloud.VERSION_NONEXISTENT) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 523 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 524 | op.setStatus(STATUS.FAILED); |
| 525 | } else { |
| 526 | op.setStatus(STATUS.SUCCESS); |
| 527 | } |
| 528 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 529 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 530 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | private boolean multiWriteInternal(final ArrayList<RCMultiEntryOperation> ops) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 534 | boolean failExists = false; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 535 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 536 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 537 | final int reqs = ops.size(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 538 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 539 | MultiWriteObject multiWriteObjects = new MultiWriteObject(reqs); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 540 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 541 | for (int i = 0; i < reqs; ++i) { |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 542 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 543 | IModifiableMultiEntryOperation op = ops.get(i); |
| 544 | RejectRules rules = new RejectRules(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 545 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 546 | switch (op.getOperation()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 547 | case CREATE: |
| 548 | rules.rejectIfExists(); |
| 549 | break; |
| 550 | case FORCE_CREATE: |
| 551 | // no reject rule |
| 552 | break; |
| 553 | case UPDATE: |
| 554 | rules.rejectIfDoesntExists(); |
| 555 | rules.rejectIfNeVersion(op.getVersion()); |
| 556 | break; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 557 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 558 | default: |
| 559 | log.error("Invalid operation {} specified on multiWriteInternal", op.getOperation()); |
| 560 | failExists = true; |
| 561 | op.setStatus(STATUS.FAILED); |
| 562 | return failExists; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 563 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 564 | multiWriteObjects.setObject(i, ((RCTableID) op.getTableId()).getTableID(), op.getKey(), op.getValue(), rules); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 565 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 566 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 567 | MultiWriteRspObject[] results = rcClient.multiWrite(multiWriteObjects.tableId, multiWriteObjects.key, multiWriteObjects.keyLength, multiWriteObjects.value, multiWriteObjects.valueLength, ops.size(), multiWriteObjects.rules); |
| 568 | if (results.length != reqs) { |
| 569 | log.error("multiWrite returned unexpected number of results. (requested:{}, returned:{})", reqs, results.length); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 570 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 571 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 572 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 573 | for (int i = 0; i < results.length; ++i) { |
| 574 | IModifiableMultiEntryOperation op = ops.get(i); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 575 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 576 | if (results[i] != null |
| 577 | && results[i].getStatus() == RCClient.STATUS_OK) { |
| 578 | op.setStatus(STATUS.SUCCESS); |
| 579 | op.setVersion(results[i].getVersion()); |
| 580 | } else { |
| 581 | op.setStatus(STATUS.FAILED); |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 582 | failExists = true; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 583 | } |
| 584 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 585 | |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 586 | return failExists; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 589 | private static final ConcurrentHashMap<String, RCTable> TABLES = new ConcurrentHashMap<>(); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 590 | |
| 591 | @Override |
| 592 | public IKVTable getTable(final String tableName) { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 593 | RCTable table = TABLES.get(tableName); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 594 | if (table == null) { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 595 | RCTable newTable = new RCTable(tableName); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 596 | RCTable existingTable = TABLES |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 597 | .putIfAbsent(tableName, newTable); |
| 598 | if (existingTable != null) { |
| 599 | return existingTable; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 600 | } else { |
Yuta HIGUCHI | a14eb17 | 2014-03-24 15:03:23 -0700 | [diff] [blame] | 601 | return newTable; |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
| 604 | return table; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void dropTable(IKVTable table) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 609 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 610 | rcClient.dropTable(table.getTableId().getTableName()); |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 611 | TABLES.remove(table.getTableId().getTableName()); |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | static final long VERSION_NONEXISTENT = JRamCloud.VERSION_NONEXISTENT; |
| 615 | |
| 616 | @Override |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 617 | public long getVersionNonexistant() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 618 | return VERSION_NONEXISTENT; |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 619 | } |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 620 | |
| 621 | @Override |
| 622 | public void createCounter(final IKVTableID tableId, final byte[] key, |
| 623 | final long initialValue) |
| 624 | throws ObjectExistsException { |
| 625 | |
| 626 | ByteBuffer valueBytes = ByteBuffer.allocate(8) |
| 627 | .order(ByteOrder.LITTLE_ENDIAN).putLong(initialValue); |
| 628 | valueBytes.flip(); |
| 629 | final long version = create(tableId, key, valueBytes.array()); |
| 630 | if (log.isTraceEnabled()) { |
| 631 | log.trace("Created counter {}-{}={}@{}", |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 632 | tableId, ByteArrayUtil.toHexStringBuilder(key, ":"), |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 633 | initialValue, version); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | @Override |
| 638 | public void setCounter(final IKVTableID tableId, final byte[] key, |
| 639 | final long value) { |
| 640 | |
| 641 | ByteBuffer valueBytes = ByteBuffer.allocate(8) |
| 642 | .order(ByteOrder.LITTLE_ENDIAN).putLong(value); |
| 643 | valueBytes.flip(); |
| 644 | |
| 645 | final long version = forceCreate(tableId, key, valueBytes.array()); |
| 646 | if (log.isTraceEnabled()) { |
| 647 | log.trace("set counter {}-{}={}@{}", |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 648 | tableId, ByteArrayUtil.toHexStringBuilder(key, ":"), |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 649 | value, version); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | @Override |
| 654 | public long incrementCounter(final IKVTableID tableId, final byte[] key, |
| 655 | final long incrementValue) { |
| 656 | |
| 657 | RCTableID rcTableId = (RCTableID) tableId; |
| 658 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 659 | |
| 660 | try { |
| 661 | return rcClient.increment(rcTableId.getTableID(), key, incrementValue); |
| 662 | } catch (JRamCloud.ObjectDoesntExistException e) { |
| 663 | log.warn("Counter {}-{} was not present", |
| 664 | tableId, |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 665 | ByteArrayUtil.toHexStringBuilder(key, ":")); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 666 | try { |
| 667 | // creating counter initialized to 0 |
| 668 | createCounter(rcTableId, key, 0L); |
| 669 | } catch (ObjectExistsException e1) { |
| 670 | // someone concurrently created it |
| 671 | log.debug("Counter {}-{} seemed to be concurrently created.", |
| 672 | tableId, |
Yuta HIGUCHI | 805bc8f | 2014-04-16 11:51:43 -0700 | [diff] [blame] | 673 | ByteArrayUtil.toHexStringBuilder(key, ":")); |
Yuta HIGUCHI | d47eac3 | 2014-04-07 13:44:47 -0700 | [diff] [blame] | 674 | } |
| 675 | try { |
| 676 | return rcClient.increment(rcTableId.getTableID(), key, incrementValue); |
| 677 | } catch (edu.stanford.ramcloud.JRamCloud.ObjectDoesntExistException e1) { |
| 678 | log.error("Should never happen"); |
| 679 | throw new IllegalStateException("Created counter disappeared."); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public void destroyCounter(final IKVTableID tableId, final byte[] key) { |
| 686 | |
| 687 | RCTableID rcTableId = (RCTableID) tableId; |
| 688 | JRamCloud rcClient = RCClient.getJRamCloudClient(); |
| 689 | |
| 690 | rcClient.remove(rcTableId.getTableID(), key); |
| 691 | } |
| 692 | |
| 693 | @Override |
| 694 | public long getCounter(IKVTableID tableId, byte[] key) |
| 695 | throws ObjectDoesntExistException { |
| 696 | |
| 697 | IKVEntry entry = read(tableId, key); |
| 698 | ByteBuffer counter = ByteBuffer.wrap(entry.getValue()).order(ByteOrder.LITTLE_ENDIAN); |
| 699 | return counter.getLong(); |
| 700 | } |
Yuta HIGUCHI | 66ca1bf | 2014-03-12 18:34:09 -0700 | [diff] [blame] | 701 | } |