private void init(HazelcastInstance hz, DefaultProperties slaveProperties) throws IOException, InterruptedException { IAtomicLong nodeId = hz.getAtomicLong(NODE_ID); id = nodeId.getAndIncrement(); prepLatch = hz.getCountDownLatch(PREP_LATCH); if(id < 1) { prepLatch.trySetCount(1); } prepLatch.await(DEFAULT_WAIT_UNIT, TimeUnit.MINUTES); execLatch = hz.getCountDownLatch(EXEC_LATCH); configMap = hz.getMap(CONF_MAP); DefaultProperties masterProperties = (DefaultProperties) configMap.get(DEFAULT_PROPS); DefaultProperties merged = DefaultProperties.merge(slaveProperties, masterProperties); executeSend(merged); LOGGER.info("Leader finished"); }
private void init(HazelcastInstance hz, DefaultProperties properties, InsertProperties insProperties) throws IOException, InterruptedException { IAtomicLong nodeId = hz.getAtomicLong(NODE_ID); id = nodeId.getAndIncrement(); prepLatch = hz.getCountDownLatch(PREP_LATCH); if(id < 1) { prepLatch.trySetCount(SINGLE_PREP_LATCH_VALUE); } execLatch = hz.getCountDownLatch(EXEC_LATCH); int numOfNodes = insProperties.getClusterNodes(); LOGGER.info("Establishing master node... Waiting for {} nodes (including master node)", numOfNodes); Validate.isTrue(execLatch.trySetCount(numOfNodes), "Execution latch could not be set"); IMap<String, Object> configMap = hz.getMap(CONF_MAP); configMap.put(DEFAULT_PROPS, properties); Reader reader = guessReader(insProperties.getWorkloadLocation()); String workloadAsText = IOUtils.toString(reader); configMap.put(WORKLOAD, workloadAsText); prepLatch.countDown(); }
private void test(final HazelcastInstance hazelcast1, final HazelcastInstance hazelcast2) { final IAtomicLong counterA = hazelcast1.getAtomicLong("counter-a"); final long countA = counterA.incrementAndGet(); LOG.log(INFO, "expecting {0} = {1}", new Object[]{counterA.get(), hazelcast1.getAtomicLong("counter-a").get()}); assertThat(countA, is(hazelcast1.getAtomicLong("counter-a").get())); assertThat(hazelcast2.getAtomicLong("counter-a").get(), is(hazelcast1.getAtomicLong("counter-a").get())); final Member member2 = hazelcast2.getCluster().getLocalMember(); LOG.log(INFO, member2.getAttributes().entrySet().stream() .map(entry -> entry.getKey() + " -> " + entry.getValue() + " : " + entry.getValue().getClass().getName()) .collect(Collectors.joining("\n", "member 2 attributes\n", "\n")) ); assertThat(member2.getStringAttribute("string"), is("hello")); assertThat(member2.getIntAttribute("int"), is(1)); assertThat(member2.getBooleanAttribute("bool"), is(true)); }
public static void main( String[] args ) throws FileNotFoundException, InterruptedException { Config config = new Config(); HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config); IAtomicLong atomicLong = hazelcastInstance.getAtomicLong("soy productor"); boolean cambiado = atomicLong.compareAndSet(0, 1); if (cambiado){ produce(hazelcastInstance); } else { consume(hazelcastInstance); } }
/** * Creates and returns a new producer. * * @param cloudNode the cloud node which runs this new producer * @return a new producer */ private Runnable createProducer(final CloudNode cloudNode) { return () -> { logger.info("Producer thread started."); while (this.isMaster(cloudNode)) { try { final Set<String> nodes = new HashSet<>(HazelcastHelper.this.getActiveNodes()); nodes.remove(cloudNode.getName()); if (nodes.size() > 0) { final IAtomicLong serialNumber = getAtomicLong(TASK_NUMBER); final String taskName = String.format("TASK-%d-%d", serialNumber.incrementAndGet(), System.currentTimeMillis()); HazelcastHelper.this.getTaskQueue().put(taskName); logger.info("Added task {}", taskName); } Thread.sleep(RandomUtils.nextInt(1500, 4000)); } catch (final Exception ex) { logger.error("Exception occurred!", ex); } } }; }
@Override public IAtomicLong getIAtomicLong(String name) { name = Objects.requireNonNull(name); final IAtomicLong valu = getBeanSafely(name, IAtomicLong.class); if (null != valu) { return valu; } return hz().getAtomicLong(name); }
/** * {@inheritDoc} * <p /> * Warning: The counter is a different object from {@code key} entry on * IKVTable with {@code tableId}. You cannot use table API to read/write * counters. * * @param tableId Only getTableName() will be used. * @param key tableId + key will be used as Counter name */ @Override public void createCounter(final IKVTableID tableId, final byte[] key, final long initialValue) throws ObjectExistsException { IAtomicLong counter = getAtomicLong(tableId, key); // Assumption here is that AtomicLong is initialized to 0L final boolean success = counter.compareAndSet(0L, initialValue); if (!success) { throw new ObjectExistsException("Atomic counter " + getCounterName(tableId, key) + " already exist with value:" + counter.get()); } }
@Override public void setCounter(final IKVTableID tableId, final byte[] key, final long value) { IAtomicLong counter = getAtomicLong(tableId, key); counter.set(value); }
@Override public long incrementCounter(final IKVTableID tableId, final byte[] key, final long incrementValue) { IAtomicLong counter = getAtomicLong(tableId, key); return counter.addAndGet(incrementValue); }
@Override public long getCounter(final IKVTableID tableId, final byte[] key) throws ObjectDoesntExistException { IAtomicLong counter = getAtomicLong(tableId, key); return counter.get(); }
@Teardown public void teardown() { if (isMemberNode(targetInstance)) { for (IAtomicLong counter : counters) { counter.destroy(); } } totalCounter.destroy(); logger.info(getOperationCountInformation(targetInstance)); }
@Setup public void setup() { totalCounter = targetInstance.getAtomicLong(name + ":TotalCounter"); counters = new IAtomicLong[countersLength]; String[] names = generateStringKeys(name, countersLength, keyLocality, targetInstance); for (int i = 0; i < countersLength; i++) { counters[i] = targetInstance.getAtomicLong(names[i]); } }
@Teardown public void teardown() { for (IAtomicLong counter : counters) { counter.destroy(); } totalCounter.destroy(); }
@Prepare(global = true) public void prepare() { for (int i = 0; i < maxAccounts; i++) { IAtomicLong account = targetInstance.getAtomicLong(name + i); account.set(INITIAL_VALUE); } totalValue = INITIAL_VALUE * maxAccounts; }
@TimeStep public void timeStep(BaseThreadState state) { int key1 = state.randomInt(maxAccounts); int key2; do { key2 = state.randomInt(maxAccounts); } while (key1 == key2); ILock lock1 = targetInstance.getLock(name + key1); if (lock1.tryLock()) { try { ILock lock2 = targetInstance.getLock(name + key2); if (lock2.tryLock()) { try { IAtomicLong account1 = targetInstance.getAtomicLong(name + key1); IAtomicLong account2 = targetInstance.getAtomicLong(name + key2); int delta = state.randomInt(100); if (account1.get() >= delta) { account1.set(account1.get() - delta); account2.set(account2.get() + delta); } } finally { lock2.unlock(); } } } finally { lock1.unlock(); } } }
@Verify public void verify() { int value = 0; for (int i = 0; i < maxAccounts; i++) { ILock lock = targetInstance.getLock(name + i); IAtomicLong account = targetInstance.getAtomicLong(name + i); logger.info(format("%s %d", account, account.get())); assertFalse(name + ": Lock should be unlocked", lock.isLocked()); assertTrue(name + ": Amount is < 0 ", account.get() >= 0); value += account.get(); } assertEquals(name + " totals not adding up ", totalValue, value); }
@Prepare(global = true) public void prepare() { for (int i = 0; i < lockCount; i++) { long key = lockCounter.getAndIncrement(); targetInstance.getLock(getLockId(key)); IAtomicLong account = targetInstance.getAtomicLong(getAccountId(key)); account.set(initialAmount); } }
@TimeStep public void timeStep(ThreadState state) { long key1 = state.getRandomAccountKey(); long key2 = state.getRandomAccountKey(); int randomAmount = state.randomInt(amount); ILock lock1 = targetInstance.getLock(getLockId(key1)); ILock lock2 = targetInstance.getLock(getLockId(key2)); IAtomicLong account1 = targetInstance.getAtomicLong(getAccountId(key1)); IAtomicLong account2 = targetInstance.getAtomicLong(getAccountId(key2)); if (!lock1.tryLock()) { return; } try { if (!lock2.tryLock()) { return; } try { if (account1.get() < 0 || account2.get() < 0) { throw new TestException("Amount on account can't be smaller than 0"); } if (account1.get() < randomAmount) { return; } account1.set(account1.get() - randomAmount); account2.set(account2.get() + randomAmount); } finally { lock2.unlock(); } } finally { lock1.unlock(); } }
@Override public IAtomicLong getAtomicLong(String s) { return null; }
public void setPathGen(IAtomicLong pathGen) { this.pathGen = new IdGeneratorImpl(pathGen); }
public IdGeneratorImpl(IAtomicLong idGen) { this.idGen = idGen; }
public HazelcastAtomicLong(IAtomicLong delegate) { this.delegate = delegate; }
@Override public IAtomicLong getAtomicLong(final String name) { return getHazelcastInstance().getAtomicLong(name); }
public HazelcastInternalAsyncCounter(Vertx vertx, IAtomicLong atomicLong) { this.vertx = vertx; this.atomicLong = atomicLong; }
private HazelcastCounter(IAtomicLong atomicLong) { this.atomicLong = atomicLong; }
private IAtomicLong getAtomicLong(final IKVTableID tableId, final byte[] key) { // TODO we probably want to implement some sort of caching return hazelcastInstance.getAtomicLong(getCounterName(tableId, key)); }
@Override public void destroyCounter(final IKVTableID tableId, final byte[] key) { IAtomicLong counter = getAtomicLong(tableId, key); counter.destroy(); }
@Override public SeqNum getLatestSnapShotId(SharedLogObjectID oid) { IAtomicLong latest = getLatestSnapshotStore(oid); final long ssId = latest.get(); return SeqNum.anyValueOf(ssId); }
private IAtomicLong randomCounter() { int index = randomInt(counters.length); return counters[index]; }
/** * Returns the atomic long integer related with the given name. * * @param numberName the atomic long integer * @return the name of atomic long integer * @throws NullPointerException if the given name is {@code null} */ private static IAtomicLong getAtomicLong(final String numberName) { Objects.requireNonNull(numberName, "The given atomic integer name must not be null."); return Holder.INSTANCE.getInstance().getAtomicLong(numberName); }
/** * Gets the store holding latest snapshot version. * * @param oid {@link SharedLogObjectID} * @return IAtomicLong */ private IAtomicLong getLatestSnapshotStore(SharedLogObjectID oid) { return instance.getAtomicLong(LATEST_SNAPSHOT_PREFIX + oid.getObjectName()); }
/** * Constructor. * * @param value IAtomicLong to use as Sequencer */ public HazelcastSequencer(IAtomicLong value) { this.value = checkNotNull(value); }
public IAtomicLong getIAtomicLong(String name);