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