Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.hazelcast; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Arrays; |
| 6 | import java.util.List; |
| 7 | import java.util.Set; |
| 8 | import java.util.concurrent.atomic.AtomicLong; |
| 9 | |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 10 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 11 | |
Jonathan Hart | 6df9017 | 2014-04-03 10:13:11 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.datastore.IKVTable; |
| 13 | import net.onrc.onos.core.datastore.IKVTableID; |
| 14 | import net.onrc.onos.core.datastore.ObjectDoesntExistException; |
| 15 | import net.onrc.onos.core.datastore.ObjectExistsException; |
| 16 | import net.onrc.onos.core.datastore.WrongVersionException; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 17 | |
| 18 | import org.slf4j.Logger; |
| 19 | import org.slf4j.LoggerFactory; |
| 20 | |
| 21 | import com.hazelcast.core.IMap; |
| 22 | import com.hazelcast.nio.ObjectDataInput; |
| 23 | import com.hazelcast.nio.ObjectDataOutput; |
| 24 | import com.hazelcast.nio.serialization.IdentifiedDataSerializable; |
| 25 | |
| 26 | public class HZTable implements IKVTable, IKVTableID { |
| 27 | @SuppressWarnings("unused") |
| 28 | private static final Logger log = LoggerFactory.getLogger(HZTable.class); |
| 29 | |
| 30 | // not sure how strict this should be managed |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 31 | private static final AtomicLong INITIAL_VERSION = new AtomicLong(HZClient.VERSION_NONEXISTENT); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * generate a new initial version for an entry. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 36 | * @return initial value |
| 37 | */ |
| 38 | protected static long getInitialVersion() { |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 39 | long version = INITIAL_VERSION.incrementAndGet(); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 40 | if (version == HZClient.VERSION_NONEXISTENT) { |
| 41 | // used up whole 64bit space? |
Ray Milkey | 5c9f2db | 2014-04-09 10:31:21 -0700 | [diff] [blame] | 42 | version = INITIAL_VERSION.incrementAndGet(); |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 43 | } |
| 44 | return version; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /** |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 48 | * increment version, avoiding versionNonexistant. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | * |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 50 | * @param version |
| 51 | * @return |
| 52 | */ |
| 53 | protected static long getNextVersion(final long version) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 54 | long nextVersion = version + 1; |
| 55 | if (nextVersion == HZClient.VERSION_NONEXISTENT) { |
| 56 | ++nextVersion; |
| 57 | } |
| 58 | return nextVersion; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static class VersionedValue implements IdentifiedDataSerializable { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 62 | private static final long serialVersionUID = -3149375966890712708L; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 63 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 64 | private byte[] value; |
| 65 | private long version; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 66 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 67 | protected VersionedValue() { |
| 68 | value = new byte[0]; |
| 69 | version = HZClient.VERSION_NONEXISTENT; |
| 70 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 71 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 72 | public VersionedValue(final byte[] value, final long version) { |
| 73 | this.value = value; |
| 74 | this.version = version; |
| 75 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 76 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 77 | public byte[] getValue() { |
| 78 | return value; |
| 79 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 80 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 81 | public long getVersion() { |
| 82 | return version; |
| 83 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 84 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 85 | public void setValue(final byte[] value) { |
| 86 | this.value = value; |
| 87 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 88 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 89 | public void setNextVersion() { |
| 90 | this.version = getNextVersion(this.version); |
| 91 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 92 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public void writeData(final ObjectDataOutput out) throws IOException { |
| 95 | out.writeLong(version); |
| 96 | out.writeInt(value.length); |
| 97 | if (value.length > 0) { |
| 98 | out.write(value); |
| 99 | } |
| 100 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 101 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public void readData(final ObjectDataInput in) throws IOException { |
| 104 | version = in.readLong(); |
| 105 | final int valueLen = in.readInt(); |
| 106 | value = new byte[valueLen]; |
| 107 | in.readFully(value); |
| 108 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 109 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public int getFactoryId() { |
| 112 | return VersionedValueSerializableFactory.FACTORY_ID; |
| 113 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 114 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 115 | @Override |
| 116 | public int getId() { |
| 117 | return VersionedValueSerializableFactory.VERSIONED_VALUE_ID; |
| 118 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 119 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 120 | @Override |
| 121 | public int hashCode() { |
| 122 | final int prime = 31; |
| 123 | int result = 1; |
| 124 | result = prime * result + (int) (version ^ (version >>> 32)); |
| 125 | result = prime * result + Arrays.hashCode(value); |
| 126 | return result; |
| 127 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 128 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 129 | @Override |
| 130 | public boolean equals(final Object obj) { |
| 131 | if (this == obj) { |
| 132 | return true; |
| 133 | } |
| 134 | if (obj == null) { |
| 135 | return false; |
| 136 | } |
| 137 | if (getClass() != obj.getClass()) { |
| 138 | return false; |
| 139 | } |
| 140 | VersionedValue other = (VersionedValue) obj; |
| 141 | if (version != other.version) { |
| 142 | return false; |
| 143 | } |
| 144 | if (!Arrays.equals(value, other.value)) { |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
| 148 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // TODO Refactor and extract common parts |
| 152 | public static class Entry implements IKVEntry { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 153 | final byte[] key; |
| 154 | byte[] value; |
| 155 | long version; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 156 | |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 157 | @SuppressFBWarnings(value = "EI_EXPOSE_REP2", |
| 158 | justification = "TODO: Store a copy of the object?") |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 159 | public Entry(final byte[] key, final byte[] value, final long version) { |
| 160 | this.key = key; |
| 161 | this.setValue(value); |
| 162 | this.setVersion(version); |
| 163 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 164 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 165 | public Entry(final byte[] key) { |
| 166 | this(key, null, HZClient.VERSION_NONEXISTENT); |
| 167 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 168 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 169 | @Override |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 170 | @SuppressFBWarnings(value = "EI_EXPOSE_REP", |
| 171 | justification = "TODO: Return a copy of the object?") |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 172 | public byte[] getKey() { |
| 173 | return key; |
| 174 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 175 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 176 | @Override |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 177 | @SuppressFBWarnings(value = "EI_EXPOSE_REP", |
| 178 | justification = "TODO: Return a copy of the object?") |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 179 | public byte[] getValue() { |
| 180 | return value; |
| 181 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 182 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public long getVersion() { |
| 185 | return version; |
| 186 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 187 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 188 | void setValue(final byte[] value) { |
| 189 | this.value = value; |
| 190 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 191 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 192 | void setVersion(final long version) { |
| 193 | this.version = version; |
| 194 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 198 | private final String mapName; |
| 199 | private final IMap<byte[], VersionedValue> map; |
| 200 | |
| 201 | public HZTable(final String mapName, final IMap<byte[], VersionedValue> map) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 202 | this.mapName = mapName; |
| 203 | this.map = map; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public String getTableName() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 208 | return mapName; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public IKVTableID getTableId() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 213 | return this; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public long create(final byte[] key, final byte[] value) throws ObjectExistsException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 218 | final long version = getInitialVersion(); |
| 219 | VersionedValue existing = map.putIfAbsent(key, new VersionedValue(value, version)); |
| 220 | if (existing != null) { |
| 221 | throw new ObjectExistsException(this, key); |
| 222 | } |
| 223 | return version; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public long forceCreate(final byte[] key, final byte[] value) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 228 | final long version = getInitialVersion(); |
| 229 | map.set(key, new VersionedValue(value, version)); |
| 230 | return version; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public IKVEntry read(final byte[] key) throws ObjectDoesntExistException { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 235 | final VersionedValue value = map.get(key); |
| 236 | if (value == null) { |
| 237 | throw new ObjectDoesntExistException(this, key); |
| 238 | } |
| 239 | return new Entry(key, value.getValue(), value.getVersion()); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public long update(final byte[] key, final byte[] value, final long version) |
| 244 | throws ObjectDoesntExistException, WrongVersionException { |
| 245 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 246 | try { |
| 247 | map.lock(key); |
| 248 | final VersionedValue oldValue = map.get(key); |
| 249 | if (oldValue == null) { |
| 250 | throw new ObjectDoesntExistException(this, key); |
| 251 | } |
| 252 | if (oldValue.getVersion() != version) { |
| 253 | throw new WrongVersionException(this, key, version, oldValue.getVersion()); |
| 254 | } |
| 255 | final long nextVersion = getNextVersion(version); |
| 256 | map.set(key, new VersionedValue(value, nextVersion)); |
| 257 | return nextVersion; |
| 258 | } finally { |
| 259 | map.unlock(key); |
| 260 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public long update(final byte[] key, final byte[] value) |
| 265 | throws ObjectDoesntExistException { |
| 266 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 267 | try { |
| 268 | map.lock(key); |
| 269 | final VersionedValue valueInMap = map.get(key); |
| 270 | if (valueInMap == null) { |
| 271 | throw new ObjectDoesntExistException(this, key); |
| 272 | } |
| 273 | valueInMap.setValue(value); |
| 274 | valueInMap.setNextVersion(); |
| 275 | map.set(key, valueInMap); |
| 276 | return valueInMap.getVersion(); |
| 277 | } finally { |
| 278 | map.unlock(key); |
| 279 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public long delete(final byte[] key, final long version) |
| 284 | throws ObjectDoesntExistException, WrongVersionException { |
| 285 | |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 286 | try { |
| 287 | map.lock(key); |
| 288 | final VersionedValue oldValue = map.get(key); |
| 289 | if (oldValue == null) { |
| 290 | throw new ObjectDoesntExistException(this, key); |
| 291 | } |
| 292 | if (oldValue.getVersion() != version) { |
| 293 | throw new WrongVersionException(this, key, version, oldValue.getVersion()); |
| 294 | } |
| 295 | map.delete(key); |
| 296 | return oldValue.getVersion(); |
| 297 | } finally { |
| 298 | map.unlock(key); |
| 299 | } |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public long forceDelete(final byte[] key) { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 304 | final VersionedValue valueInMap = map.remove(key); |
| 305 | if (valueInMap == null) { |
| 306 | return HZClient.VERSION_NONEXISTENT; |
| 307 | } |
| 308 | return valueInMap.getVersion(); |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public Iterable<IKVEntry> getAllEntries() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 313 | final Set<IMap.Entry<byte[], VersionedValue>> entries = map.entrySet(); |
| 314 | List<IKVEntry> entryList = new ArrayList<IKVTable.IKVEntry>(entries.size()); |
| 315 | for (IMap.Entry<byte[], VersionedValue> entry : entries) { |
| 316 | entryList.add(new Entry(entry.getKey(), entry.getValue().getValue(), entry.getValue().getVersion())); |
| 317 | } |
| 318 | return entryList; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public String toString() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 323 | return "[HZTable " + mapName + "]"; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | IMap<byte[], VersionedValue> getBackendMap() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 327 | return this.map; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
Ray Milkey | 7531a34 | 2014-04-11 15:08:12 -0700 | [diff] [blame] | 331 | public long getVersionNonexistant() { |
Yuta HIGUCHI | 826b4a4 | 2014-03-24 13:10:33 -0700 | [diff] [blame] | 332 | return HZClient.VERSION_NONEXISTENT; |
Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame] | 333 | } |
| 334 | } |