public RFuture<Void> offerAsync(V e, long delay, TimeUnit timeUnit) { long delayInMs = timeUnit.toMillis(delay); long timeout = System.currentTimeMillis() + delayInMs; long randomId = ThreadLocalRandom.current().nextLong(); return commandExecutor.evalWriteAsync(getName(), codec, RedisCommands.EVAL_VOID, "local value = struct.pack('dLc0', tonumber(ARGV[2]), string.len(ARGV[3]), ARGV[3]);" + "redis.call('zadd', KEYS[2], ARGV[1], value);" + "redis.call('rpush', KEYS[3], value);" // if new object added to queue head when publish its startTime // to all scheduler workers + "local v = redis.call('zrange', KEYS[2], 0, 0); " + "if v[1] == value then " + "redis.call('publish', KEYS[4], ARGV[1]); " + "end;" , Arrays.<Object>asList(getName(), getTimeoutSetName(), getQueueName(), getChannelName()), timeout, randomId, encode(e)); }
public void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException { long waitTime = -1; if (leaseTime == -1) { waitTime = 5; unit = TimeUnit.SECONDS; } else { waitTime = unit.toMillis(leaseTime); if (waitTime <= 2000) { waitTime = 2000; } else if (waitTime <= 5000) { waitTime = ThreadLocalRandom.current().nextLong(waitTime/2, waitTime); } else { waitTime = ThreadLocalRandom.current().nextLong(5000, waitTime); } waitTime = unit.convert(waitTime, TimeUnit.MILLISECONDS); } while (true) { if (tryLock(waitTime, leaseTime, unit)) { return; } } }
public void initPluginColors(Iterable<String> plugins, Map<String, String> configColors, String def) { Color[] colors = Color.values(); //remove black, because it's often hard to read colors = Arrays.copyOfRange(colors, 1, colors.length); ImmutableMap.Builder<String, String> colorBuilder = ImmutableMap.builder(); for (String plugin : plugins) { String styleCode = configColors.getOrDefault(plugin, def); if ("random".equalsIgnoreCase(styleCode)) { //ignore default styleCode = colors[ThreadLocalRandom.current().nextInt(colors.length - 1)].name(); } colorBuilder.put(plugin, format(styleCode)); } this.pluginColors = colorBuilder.build(); }
private void testInternalNioBuffer(int a) { ByteBuf buffer = releaseLater(newBuffer(2)); ByteBuffer buf = buffer.internalNioBuffer(0, 1); assertEquals(1, buf.remaining()); byte[] data = new byte[a]; ThreadLocalRandom.current().nextBytes(data); buffer.writeBytes(data); buf = buffer.internalNioBuffer(0, a); assertEquals(a, buf.remaining()); for (int i = 0; i < a; i++) { assertEquals(data[i], buf.get()); } assertFalse(buf.hasRemaining()); }
/** * Returns a (probably) non-empty steal queue, if one is found * during a scan, else null. This method must be retried by * caller if, by the time it tries to use the queue, it is empty. */ private WorkQueue findNonEmptyStealQueue() { int r = ThreadLocalRandom.current().nextInt(); for (;;) { int ps = plock, m; WorkQueue[] ws; WorkQueue q; if ((ws = workQueues) != null && (m = ws.length - 1) >= 0) { for (int j = (m + 1) << 2; j >= 0; --j) { if ((q = ws[(((r - j) << 1) | 1) & m]) != null && q.base - q.top < 0) return q; } } if (plock == ps) return null; } }
@Override public Void call() { try { Random random = ThreadLocalRandom.current(); for(int i = 0; i < STEP_COUNT; i++) { long key = getKey(i, random.nextLong()); Long result = subject.get(key); assert(result.longValue() == key + MAGIC_NUMBER); } } catch(Exception e) { e.printStackTrace(); } return null; }
private static int defaultProcessId() { String value = ManagementFactory.getRuntimeMXBean().getName(); int atIndex = value.indexOf('@'); if (atIndex >= 0) { value = value.substring(0, atIndex); } int pid; try { pid = Integer.parseInt(value); } catch (NumberFormatException e) { pid = -1; } if (pid < 0 || pid > MAX_PROCESS_ID) { pid = ThreadLocalRandom.current().nextInt(MAX_PROCESS_ID + 1); logger.warn("Failed to find the current process ID; using a random value: {}", pid); } return pid; }
protected static byte[] generateLogEntryId(byte[] keyHash) { byte[] result = new byte[keyHash.length + 1 + 8]; result[16] = ':'; byte[] id = new byte[8]; // TODO JDK UPGRADE replace to native ThreadLocalRandom ThreadLocalRandom.current().nextBytes(id); System.arraycopy(keyHash, 0, result, 0, keyHash.length); System.arraycopy(id, 0, result, 17, id.length); return result; }
private boolean removeValue(K key) { double syncId = ThreadLocalRandom.current().nextDouble(); List<Object> res = evalWrite(getName(), codec, RedisCommands.EVAL_LIST, "local value = redis.call('hexists', KEYS[1], ARGV[2]); " + "if value == 0 then " + "return {0}; " + "end; " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore); " + "end; " + "if expireDate <= tonumber(ARGV[1]) then " + "return {0}; " + "end; " + "value = redis.call('hget', KEYS[1], ARGV[2]); " + "redis.call('hdel', KEYS[1], ARGV[2]); " + "redis.call('zrem', KEYS[2], ARGV[2]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[2]), ARGV[2], string.len(tostring(value)), tostring(value), ARGV[3]); " + "local syncs = redis.call('publish', KEYS[4], syncMsg); " + "return {1, syncs};", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getRemovedSyncChannelName()), System.currentTimeMillis(), encodeMapKey(key), syncId); res.add(syncId); waitSync(res); return (Long) res.get(0) == 1; }
private V getAndRemoveValue(K key) { double syncId = ThreadLocalRandom.current().nextDouble(); List<Object> result = evalWrite(getName(), codec, RedisCommands.EVAL_MAP_VALUE, "local value = redis.call('hget', KEYS[1], ARGV[2]); " + "if value == false then " + "return {nil}; " + "end; " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[2]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore); " + "end; " + "if expireDate <= tonumber(ARGV[1]) then " + "return {nil}; " + "end; " + "redis.call('hdel', KEYS[1], ARGV[2]); " + "redis.call('zrem', KEYS[2], ARGV[2]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[2]), ARGV[2], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[2]), ARGV[2], string.len(tostring(value)), tostring(value), ARGV[3]); " + "local syncs = redis.call('publish', KEYS[4], syncMsg); " + "return {value, syncs}; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getRemovedSyncChannelName()), System.currentTimeMillis(), encodeMapKey(key), syncId); if (result.isEmpty()) { return null; } result.add(syncId); waitSync(result); return (V) result.get(0); }
private boolean replaceValueLocked(K key, V value) { if (containsKey(key)) { double syncId = ThreadLocalRandom.current().nextDouble(); Long updateTimeout = getUpdateTimeout(); Long syncs = evalWrite(getName(), codec, RedisCommands.EVAL_LONG, "if ARGV[1] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[3]); " + "redis.call('zrem', KEYS[2], ARGV[3]); " + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value), ARGV[5]); " + "return redis.call('publish', KEYS[5], syncMsg); " + "elseif ARGV[1] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], ARGV[5]); " + "return redis.call('publish', KEYS[6], syncMsg); " + "else " + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], ARGV[5]); " + "return redis.call('publish', KEYS[6], syncMsg); " + "end; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getUpdatedChannelName(), getRemovedSyncChannelName(), getUpdatedSyncChannelName()), updateTimeout, System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value), syncId); List<Object> result = Arrays.<Object>asList(syncs, syncId); waitSync(result); return true; } return false; }
public PlayedNote(Instrument instrument, int startTick, int duration, int key, SoundCategory category, Object noteLocation) { this.instrument = instrument; this.key = key; this.startTick = startTick; this.duration = duration; this.noteLocation = noteLocation; uniqueId = MathHelper.getRandomUUID(ThreadLocalRandom.current()).toString(); InstrumentTuning.TuningInfo tuning = instrument.tuning.keyToTuningMap.get(key); float pitch = (float)Math.pow(2.0D, (double)tuning.keyOffset / 12.0D); this.instrumentSound = new InstrumentSound(uniqueId, SoundEvents.BLOCK_NOTE_HARP, category, duration, (int)Math.ceil(instrument.tuning.fadeout * 20F), 0.7F * (Clef.config.instrumentVolume / 100F), pitch, noteLocation); }
/** * Initializes this socket and binds its internal udp socket to a free port. * If the socket is already initialized any invocation of this method will * result in an IllegalStateException. * * @throws SocketException Thrown in case the socket could not be initialized */ public void initialize() throws SocketException { if ( this.isInitialized() ) { throw new IllegalStateException( "Cannot re-initialized ClientSocket" ); } this.udpSocket = new Bootstrap(); this.udpSocket.group( Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup() ); this.udpSocket.channel( Epoll.isAvailable() ? EpollDatagramChannel.class : NioDatagramChannel.class ); this.udpSocket.handler( new ChannelInboundHandlerAdapter() { @Override public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception { io.netty.channel.socket.DatagramPacket packet = (io.netty.channel.socket.DatagramPacket) msg; PacketBuffer content = new PacketBuffer( packet.content() ); InetSocketAddress sender = packet.sender(); if ( !receiveDatagram( sender, content ) ) { // Push datagram to update queue: handleDatagram( sender, content, System.currentTimeMillis() ); } } } ); try { this.channel = this.udpSocket.bind( ThreadLocalRandom.current().nextInt( 45000, 65000 ) ).sync().channel(); } catch ( InterruptedException e ) { SocketException exception = new SocketException( "Could not bind to socket" ); exception.initCause( e ); throw exception; } this.afterInitialize(); }
void configureHttpReceiver(final Vertx vertx, final TestContext context, final List<String> requestsWithIgnore) { httpReceiver.requestHandler(request -> { // create a delay to simulate a lengthy api call long delay = ThreadLocalRandom.current().nextInt(200, 1000 + 1); vertx.setTimer(delay, event -> request.response().end("hey!")); }).listen(HTTP_PORT, HOST, listenResult -> { if (listenResult.succeeded()) { this.makeHttpRequests(vertx, context, requestsWithIgnore); } else { context.fail(listenResult.cause()); } }); }
private static byte[] defaultMachineId() { byte[] bestMacAddr = MacAddressUtil.bestAvailableMac(); if (bestMacAddr == null) { bestMacAddr = new byte[MacAddressUtil.MAC_ADDRESS_LENGTH]; ThreadLocalRandom.current().nextBytes(bestMacAddr); logger.warn( "Failed to find a usable hardware address from the network interfaces; using random bytes: {}", MacAddressUtil.formatAddress(bestMacAddr)); } return bestMacAddr; }
@Override public void run() { try { List<GTable> tables = new ArrayList<>(humpback.getTables()); if (tables.size() == 0) { return; } int idx = ThreadLocalRandom.current().nextInt(0, tables.size()); GTable gtable = tables.get(idx); gtable.getMemTable().compact(); } catch (Exception x) { _log.error("", x); } }
@Override public List<ItemStack> getDrops(IBlockAccess world, BlockPos pos, IBlockState state, int fortune) { List<ItemStack> ret = new ArrayList<ItemStack>(); ItemStack drops = lookup.get(state.getValue(META)); ItemStack newDropStack = new ItemStack(drops.getItem(), 1, drops.getMetadata()); ret.add(newDropStack); if (fortune > 0 && ThreadLocalRandom.current().nextInt(MathHelper.clamp(4 - fortune, 1, 4)) == 0) { ret.add(newDropStack); } return ret; }
public static int[] randomIntArray(final int length, final int range) { final int[] array = new int[length]; final Random generator = ThreadLocalRandom.current(); for (int i = 0; i < array.length; i++) { array[i] = generator.nextInt(range); } return array; }
/** * Acquires the plock lock to protect worker array and related * updates. This method is called only if an initial CAS on plock * fails. This acts as a spinlock for normal cases, but falls back * to builtin monitor to block when (rarely) needed. This would be * a terrible idea for a highly contended lock, but works fine as * a more conservative alternative to a pure spinlock. */ private int acquirePlock() { int spins = PL_SPINS, ps, nps; for (;;) { if (((ps = plock) & PL_LOCK) == 0 && U.compareAndSwapInt(this, PLOCK, ps, nps = ps + PL_LOCK)) return nps; else if (spins >= 0) { if (ThreadLocalRandom.current().nextInt() >= 0) --spins; } else if (U.compareAndSwapInt(this, PLOCK, ps, ps | PL_SIGNAL)) { synchronized (this) { if ((plock & PL_SIGNAL) != 0) { try { wait(); } catch (InterruptedException ie) { try { Thread.currentThread().interrupt(); } catch (SecurityException ignore) { } } } else notifyAll(); } } } }
public InetAddress resolve(String name) throws UnknownHostException { InetSocketAddress unresolved = InetSocketAddress.createUnresolved(name, ThreadLocalRandom.current().nextInt(65536)); try { return resolver.resolve(unresolved).get().getAddress(); } catch (InterruptedException | ExecutionException e) { UnknownHostException unknownHostException = new UnknownHostException(name); unknownHostException.setStackTrace(e.getStackTrace()); throw unknownHostException; } }
private int getNextClientKeepAliveId() { clientKeepAliveId++; if (clientKeepAliveId < 1) { clientKeepAliveId = (int) ((System.currentTimeMillis() % (60 * 1000)) << 4) + ThreadLocalRandom.current().nextInt(16) + 1; } return clientKeepAliveId; }
protected String generateRequestId() { byte[] id = new byte[16]; // TODO JDK UPGRADE replace to native ThreadLocalRandom ThreadLocalRandom.current().nextBytes(id); return ByteBufUtil.hexDump(id); }
protected String generateId() { byte[] id = new byte[16]; // TODO JDK UPGRADE replace to native ThreadLocalRandom ThreadLocalRandom.current().nextBytes(id); return ByteBufUtil.hexDump(id); }
protected static byte[] generateId() { byte[] id = new byte[16]; // TODO JDK UPGRADE replace to native ThreadLocalRandom ThreadLocalRandom.current().nextBytes(id); return id; }
V getValueLocked(K key) { V value = evalWrite(getName(), codec, RedisCommands.EVAL_MAP_VALUE, "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "if value == false then " + "return nil; " + "end; " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[3]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore); " + "end; " + "if expireDate <= tonumber(ARGV[2]) then " + "return nil; " + "end; " + "return value; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName()), 0, System.currentTimeMillis(), encodeMapKey(key)); if (value != null) { List<Object> result = new ArrayList<Object>(3); result.add(value); Long accessTimeout = getAccessTimeout(); double syncId = ThreadLocalRandom.current().nextDouble(); Long syncs = evalWrite(getName(), codec, RedisCommands.EVAL_LONG, "if ARGV[1] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[3]); " + "redis.call('zrem', KEYS[2], ARGV[3]); " + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value), ARGV[4]); " + "local syncs = redis.call('publish', KEYS[4], syncMsg); " + "return syncs;" + "elseif ARGV[1] ~= '-1' then " + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "return 0;" + "end; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getRemovedSyncChannelName()), accessTimeout, System.currentTimeMillis(), encodeMapKey(key), syncId); result.add(syncs); result.add(syncId); waitSync(result); return value; } return value; }
private boolean putValue(K key, Object value) { double syncId = ThreadLocalRandom.current().nextDouble(); Long creationTimeout = getCreationTimeout(); Long updateTimeout = getUpdateTimeout(); List<Object> res = evalWrite(getName(), codec, RedisCommands.EVAL_LIST, "if redis.call('hexists', KEYS[1], ARGV[4]) == 1 then " + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[4]); " + "redis.call('zrem', KEYS[2], ARGV[4]); " + "local value = redis.call('hget', KEYS[1], ARGV[4]);" + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[6]); " + "local syncs = redis.call('publish', KEYS[7], syncMsg); " + "return {0, syncs};" + "elseif ARGV[2] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[5], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, syncs};" + "else " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[5], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, syncs};" + "end; " + "else " + "if ARGV[1] == '0' then " + "return {0};" + "elseif ARGV[1] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[6], syncMsg); " + "return {1, syncs};" + "else " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[6], syncMsg); " + "return {1, syncs};" + "end; " + "end; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getCreatedChannelName(), getRemovedChannelName(), getUpdatedChannelName(), getCreatedSyncChannelName(), getRemovedSyncChannelName(), getUpdatedSyncChannelName()), creationTimeout, updateTimeout, System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value), syncId); res.add(syncId); waitSync(res); return (Long) res.get(0) == 1; }
private List<Object> getAndPutValue(K key, V value) { Long creationTimeout = getCreationTimeout(); Long updateTimeout = getUpdateTimeout(); double syncId = ThreadLocalRandom.current().nextDouble(); List<Object> result = evalWrite(getName(), codec, RedisCommands.EVAL_LIST, "local value = redis.call('hget', KEYS[1], ARGV[4]);" + "if value ~= false then " + "if ARGV[2] == '0' then " + "redis.call('hdel', KEYS[1], ARGV[4]); " + "redis.call('zrem', KEYS[2], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(tostring(value)), tostring(value), ARGV[6]); " + "local syncs = redis.call('publish', KEYS[6], syncMsg); " + "return {0, value, syncs};" + "elseif ARGV[2] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "redis.call('zadd', KEYS[2], ARGV[2], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[5], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, value, syncs};" + "else " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[5], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[8], syncMsg); " + "return {1, value, syncs};" + "end; " + "else " + "if ARGV[1] == '0' then " + "return {nil};" + "elseif ARGV[1] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[7], syncMsg); " + "return {1, syncs};" + "else " + "redis.call('hset', KEYS[1], ARGV[4], ARGV[5]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[4]), ARGV[4], string.len(ARGV[5]), ARGV[5], ARGV[6]); " + "local syncs = redis.call('publish', KEYS[7], syncMsg); " + "return {1, syncs};" + "end; " + "end; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getCreatedChannelName(), getUpdatedChannelName(), getRemovedSyncChannelName(), getCreatedSyncChannelName(), getUpdatedSyncChannelName()), creationTimeout, updateTimeout, System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value), syncId); if (!result.isEmpty()) { result.add(syncId); } return result; }
private V getAndReplaceValueLocked(K key, V value) { V oldValue = evalWrite(getName(), codec, RedisCommands.EVAL_MAP_VALUE, "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "if value == false then " + "return nil; " + "end; " + "local expireDate = 92233720368547758; " + "local expireDateScore = redis.call('zscore', KEYS[2], ARGV[3]); " + "if expireDateScore ~= false then " + "expireDate = tonumber(expireDateScore); " + "end; " + "if expireDate <= tonumber(ARGV[2]) then " + "return nil; " + "end; " + "return value;", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getUpdatedChannelName()), 0, System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value)); if (oldValue != null) { Long updateTimeout = getUpdateTimeout(); double syncId = ThreadLocalRandom.current().nextDouble(); Long syncs = evalWrite(getName(), codec, RedisCommands.EVAL_LONG, "if ARGV[1] == '0' then " + "local value = redis.call('hget', KEYS[1], ARGV[3]); " + "redis.call('hdel', KEYS[1], ARGV[3]); " + "redis.call('zrem', KEYS[2], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value)); " + "redis.call('publish', KEYS[3], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(tostring(value)), tostring(value), ARGV[5]); " + "return redis.call('publish', KEYS[5], msg); " + "elseif ARGV[1] ~= '-1' then " + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "redis.call('zadd', KEYS[2], ARGV[1], ARGV[3]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], ARGV[5]); " + "return redis.call('publish', KEYS[6], syncMsg); " + "else " + "redis.call('hset', KEYS[1], ARGV[3], ARGV[4]); " + "local msg = struct.pack('Lc0Lc0', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4]); " + "redis.call('publish', KEYS[4], msg); " + "local syncMsg = struct.pack('Lc0Lc0d', string.len(ARGV[3]), ARGV[3], string.len(ARGV[4]), ARGV[4], ARGV[5]); " + "return redis.call('publish', KEYS[6], syncMsg); " + "end; ", Arrays.<Object>asList(getName(), getTimeoutSetName(), getRemovedChannelName(), getUpdatedChannelName(), getRemovedSyncChannelName(), getUpdatedSyncChannelName()), updateTimeout, System.currentTimeMillis(), encodeMapKey(key), encodeMapValue(value), syncId); List<Object> result = Arrays.<Object>asList(syncs, syncId); waitSync(result); } return oldValue; }
private String generateId() { byte[] id = new byte[8]; // TODO JDK UPGRADE replace to native ThreadLocalRandom ThreadLocalRandom.current().nextBytes(id); return ByteBufUtil.hexDump(id); }
public AttributeModifier(String nameIn, double amountIn, int operationIn) { this(MathHelper.getRandomUuid(ThreadLocalRandom.current()), nameIn, amountIn, operationIn); }
public void playSound(ISound sound) { if (this.loaded) { if (this.sndSystem.getMasterVolume() <= 0.0F) { logger.debug(LOG_MARKER, "Skipped playing soundEvent: {}, master volume was zero", new Object[] {sound.getSoundLocation()}); } else { SoundEventAccessorComposite soundeventaccessorcomposite = this.sndHandler.getSound(sound.getSoundLocation()); if (soundeventaccessorcomposite == null) { logger.warn(LOG_MARKER, "Unable to play unknown soundEvent: {}", new Object[] {sound.getSoundLocation()}); } else { SoundPoolEntry soundpoolentry = soundeventaccessorcomposite.cloneEntry(); if (soundpoolentry == SoundHandler.missing_sound) { logger.warn(LOG_MARKER, "Unable to play empty soundEvent: {}", new Object[] {soundeventaccessorcomposite.getSoundEventLocation()}); } else { float f = sound.getVolume(); float f1 = 16.0F; if (f > 1.0F) { f1 *= f; } SoundCategory soundcategory = soundeventaccessorcomposite.getSoundCategory(); float f2 = this.getNormalizedVolume(sound, soundpoolentry, soundcategory); double d0 = (double)this.getNormalizedPitch(sound, soundpoolentry); ResourceLocation resourcelocation = soundpoolentry.getSoundPoolEntryLocation(); if (f2 == 0.0F) { logger.debug(LOG_MARKER, "Skipped playing sound {}, volume was zero.", new Object[] {resourcelocation}); } else { boolean flag = sound.canRepeat() && sound.getRepeatDelay() == 0; String s = MathHelper.getRandomUuid(ThreadLocalRandom.current()).toString(); if (soundpoolentry.isStreamingSound()) { this.sndSystem.newStreamingSource(false, s, getURLForSoundResource(resourcelocation), resourcelocation.toString(), flag, sound.getXPosF(), sound.getYPosF(), sound.getZPosF(), sound.getAttenuationType().getTypeInt(), f1); } else { this.sndSystem.newSource(false, s, getURLForSoundResource(resourcelocation), resourcelocation.toString(), flag, sound.getXPosF(), sound.getYPosF(), sound.getZPosF(), sound.getAttenuationType().getTypeInt(), f1); } logger.debug(LOG_MARKER, "Playing sound {} for event {} as channel {}", new Object[] {soundpoolentry.getSoundPoolEntryLocation(), soundeventaccessorcomposite.getSoundEventLocation(), s}); this.sndSystem.setPitch(s, (float)d0); this.sndSystem.setVolume(s, f2); this.sndSystem.play(s); this.playingSoundsStopTime.put(s, Integer.valueOf(this.playTime + 20)); this.playingSounds.put(s, sound); this.playingSoundPoolEntries.put(sound, soundpoolentry); if (soundcategory != SoundCategory.MASTER) { this.categorySounds.put(soundcategory, s); } if (sound instanceof ITickableSound) { this.tickableSounds.add((ITickableSound)sound); } } } } } } }