void setFailure(ShardIterator shardIt, int shardIndex, Throwable t) { // we don't aggregate shard failures on non active shards (but do keep the header counts // right) if (TransportActions.isShardNotAvailableException(t)) { return; } if (!(t instanceof BroadcastShardOperationFailedException)) { t = new BroadcastShardOperationFailedException(shardIt.shardId(), t); } Object response = shardsResponses.get(shardIndex); if (response == null) { // just override it and return shardsResponses.set(shardIndex, t); } if (!(response instanceof Throwable)) { // we should never really get here... return; } // the failure is already present, try and not override it with an exception that is less // meaningless // for example, getting illegal shard state if (TransportActions.isReadOverrideException(t)) { shardsResponses.set(shardIndex, t); } }
@Override protected SuggestResponse newResponse( SuggestRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) { int successfulShards = 0; int failedShards = 0; final Map<String, List<Suggest.Suggestion>> groupedSuggestions = new HashMap<>(); List<ShardOperationFailedException> shardFailures = null; for (int i = 0; i < shardsResponses.length(); i++) { Object shardResponse = shardsResponses.get(i); if (shardResponse == null) { // simply ignore non active shards } else if (shardResponse instanceof BroadcastShardOperationFailedException) { failedShards++; if (shardFailures == null) { shardFailures = newArrayList(); } shardFailures.add( new DefaultShardOperationFailedException( (BroadcastShardOperationFailedException) shardResponse)); } else { Suggest suggest = ((ShardSuggestResponse) shardResponse).getSuggest(); Suggest.group(groupedSuggestions, suggest); successfulShards++; } } return new SuggestResponse( new Suggest(Suggest.reduce(groupedSuggestions)), shardsResponses.length(), successfulShards, failedShards, shardFailures); }
/** * Removes the next element (at hd +1). <em>Note that this method is not concurrent, as RingBuffer * can only have 1 remover thread active at any time !</em> * * @param nullify Nulls the element in the array if true * @return T if there was a non-null element at position hd +1, or null if the element at hd+1 was * null, or hd+1 > hr. */ public T remove(boolean nullify) { long tmp = hd + 1; if (tmp > hr.get()) return null; int index = index(tmp); T element = buf.get(index); if (element == null) return null; hd = tmp; if (nullify) { long tmp_low = low; if (tmp == tmp_low + 1) buf.compareAndSet(index, element, null); else { int from = index(tmp_low + 1), length = (int) (tmp - tmp_low), capacity = capacity(); for (int i = from; i < from + length; i++) { index = i % capacity; buf.set(index, null); } } low = tmp; lock.lock(); try { buffer_full.signalAll(); } finally { lock.unlock(); } } return element; }
public void closeConnections() { int i = 0; while ((connections.length() != 0) && (connections.get(i) != null)) { removeConnection(i); i++; } }
/** * Offer two elements at the same time. * * <p>Don't use the regular offer() with this at all! * * @param first * @param second * @return */ public boolean offer(T first, T second) { final AtomicReferenceArray<Object> buffer = producerBuffer; final long p = producerIndex; final int m = producerMask; int pi = calcWrappedOffset(p + 2, m); if (null == lvElement(buffer, pi)) { pi = calcWrappedOffset(p, m); soElement(buffer, pi + 1, second); soProducerIndex(p + 2); soElement(buffer, pi, first); } else { final int capacity = buffer.length(); final AtomicReferenceArray<Object> newBuffer = new AtomicReferenceArray<>(capacity); producerBuffer = newBuffer; pi = calcWrappedOffset(p, m); soElement(newBuffer, pi + 1, second); // StoreStore soElement(newBuffer, pi, first); soNext(buffer, newBuffer); soProducerIndex(p + 2); // this ensures correctness on 32bit platforms soElement(buffer, pi, HAS_NEXT); // new buffer is visible after element is } return true; }
/** * Combine this array reduction variable at the given index with the given value using the given * operation. (This array <TT>[i]</TT>) is set to (this array <TT>[i]</TT>) <I>op</I> * (<TT>value</TT>), then (this array <TT>[i]</TT>) is returned. * * @param i Index. * @param value Value. * @param op Binary operation. * @return (This array <TT>[i]</TT>) <I>op</I> (<TT>value</TT>). */ public T reduce(int i, T value, ObjectOp<T> op) { for (; ; ) { T oldvalue = myArray.get(i); T newvalue = op.op(oldvalue, value); if (myArray.compareAndSet(i, oldvalue, newvalue)) return newvalue; } }
@Override protected TermlistResponse newResponse( TermlistRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) { int successfulShards = 0; int failedShards = 0; List<ShardOperationFailedException> shardFailures = null; Set<String> termlist = new CompactHashSet(); for (int i = 0; i < shardsResponses.length(); i++) { Object shardResponse = shardsResponses.get(i); if (shardResponse == null) { // a non active shard, ignore... } else if (shardResponse instanceof BroadcastShardOperationFailedException) { failedShards++; if (shardFailures == null) { shardFailures = newArrayList(); } shardFailures.add( new DefaultShardOperationFailedException( (BroadcastShardOperationFailedException) shardResponse)); } else { successfulShards++; if (shardResponse instanceof ShardTermlistResponse) { ShardTermlistResponse resp = (ShardTermlistResponse) shardResponse; termlist.addAll(resp.getTermList()); } } } return new TermlistResponse( shardsResponses.length(), successfulShards, failedShards, shardFailures, termlist); }
/** * Adds a new element to the buffer * * @param seqno The seqno of the element * @param element The element * @param block If true, add() will block when the buffer is full until there is space. Else, * add() will return immediately, either successfully or unsuccessfully (if the buffer is * full) * @return True if the element was added, false otherwise. */ public boolean add(long seqno, T element, boolean block) { validate(seqno); if (seqno <= hd) // seqno already delivered, includes check seqno <= low return false; if (seqno - low > capacity() && (!block || !block(seqno))) // seqno too big return false; // now we can set any slow > hd and yet not overwriting low (check #1 above) int index = index(seqno); // Fix for correctness check #1 (see doc/design/RingBuffer.txt) if (buf.get(index) != null || seqno <= hd) return false; if (!buf.compareAndSet(index, null, element)) // the element at buf[index] was already present return false; // now see if hr needs to moved forward, this can be concurrent as we may have multiple // producers for (; ; ) { long current_hr = hr.get(); long new_hr = Math.max(seqno, current_hr); if (new_hr <= current_hr || hr.compareAndSet(current_hr, new_hr)) break; } return true; }
@Override protected ClearIndicesCacheResponse newResponse( ClearIndicesCacheRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) { int successfulShards = 0; int failedShards = 0; List<ShardOperationFailedException> shardFailures = null; for (int i = 0; i < shardsResponses.length(); i++) { Object shardResponse = shardsResponses.get(i); if (shardResponse == null) { // simply ignore non active shards } else if (shardResponse instanceof BroadcastShardOperationFailedException) { failedShards++; if (shardFailures == null) { shardFailures = newArrayList(); } shardFailures.add( new DefaultShardOperationFailedException( (BroadcastShardOperationFailedException) shardResponse)); } else { successfulShards++; } } return new ClearIndicesCacheResponse( shardsResponses.length(), successfulShards, failedShards, shardFailures); }
@Override public R get(Object key) { if (!(key instanceof Integer)) return null; int i = (int) key; if (0 <= i && i < array.length()) { return array.get(i); } return null; }
/** * Get or create a trace object for this module id. Trace modules with id are cached. * * @param moduleId module id * @return the trace object */ public Trace getTrace(int moduleId) { Trace t = traces.get(moduleId); if (t == null) { t = new Trace(writer, moduleId); if (!traces.compareAndSet(moduleId, null, t)) { t = traces.get(moduleId); } } return t; }
/** {@inheritDoc} */ public E set(int index, E e) { int pos = index + FIRST_BUCKET_SIZE; int bucketInd = Integer.numberOfLeadingZeros(FIRST_BUCKET_SIZE) - Integer.numberOfLeadingZeros(pos); int idx = Integer.highestOneBit(pos) ^ pos; AtomicReferenceArray<E> bucket = buckets.get(bucketInd); while (true) { E oldV = bucket.get(idx); if (bucket.compareAndSet(idx, oldV, e)) return oldV; } }
public Message remove(long seqno) { int index=index(seqno); if(index < 0) return null; Message retval=array.get(index); if(retval != null && retval != TOMBSTONE && array.compareAndSet(index, retval, TOMBSTONE)) { num_tombstones.incrementAndGet(); return retval; } return null; }
private void testJoinWithManyNodesMultipleGroups(final boolean multicast) throws InterruptedException { final int nodeCount = 10; final int groupCount = 3; final int basePort = 12301; final CountDownLatch latch = new CountDownLatch(nodeCount); final AtomicReferenceArray<HazelcastInstance> instances = new AtomicReferenceArray<HazelcastInstance>(nodeCount); final Map<String, AtomicInteger> groups = new HashMap<String, AtomicInteger>(groupCount); for (int i = 0; i < groupCount; i++) { groups.put("group-" + i, new AtomicInteger(0)); } ExecutorService ex = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2); for (int i = 0; i < nodeCount; i++) { final int portSeed = i; ex.execute( new Runnable() { public void run() { sleepRandom(1, 1000); Config config = new Config(); String name = "group-" + (int) (Math.random() * groupCount); config.getGroupConfig().setName(name); groups.get(name).incrementAndGet(); initNetworkConfig( config.getNetworkConfig(), basePort, portSeed, multicast, nodeCount); HazelcastInstance h = Hazelcast.newHazelcastInstance(config); instances.set(portSeed, h); latch.countDown(); } }); } try { latch.await(200, TimeUnit.SECONDS); } finally { ex.shutdown(); } for (int i = 0; i < nodeCount; i++) { HazelcastInstance hz = instances.get(i); assertNotNull(hz); int clusterSize = hz.getCluster().getMembers().size(); String groupName = hz.getConfig().getGroupConfig().getName(); int shouldBeClusterSize = groups.get(groupName).get(); assertEquals(groupName + ": ", shouldBeClusterSize, clusterSize); } }
public void testNewReferenceArray_withLength() throws Exception { int length = 42; AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(length); for (int i = 0; i < length; ++i) { assertEquals(null, refArray.get(i)); } try { refArray.get(length); fail(); } catch (IndexOutOfBoundsException expected) { } }
/** @return Random node and its index. */ @Nullable private T2<Ignite, Integer> randomNode() { while (!done) { int idx = ThreadLocalRandom.current().nextInt(GRID_CNT); Ignite ignite = nodes.get(idx); if (ignite != null && nodes.compareAndSet(idx, ignite, null)) return new T2<>(ignite, idx); } return null; }
public void testNewReferenceArray_withStringArray() throws Exception { String[] array = {"foo", "bar", "baz"}; AtomicReferenceArray<String> refArray = Atomics.newReferenceArray(array); for (int i = 0; i < array.length; ++i) { assertEquals(array[i], refArray.get(i)); } try { refArray.get(array.length); fail(); } catch (IndexOutOfBoundsException expected) { } }
/** Clears any data structures and places it back to its state when it was first created. */ protected final void clear() { map.clear(); // also clear sample as chances of producing a useful result after a removeAll are 0 if (useKeySample) { // clear this. Because this is not locked, a few puts may get overwritten and be unable to be // sample // for eviction. Not a problem. for (int i = 0; i < keyArray.length(); i++) { keyArray.set(i, null); } } }
private GetFieldMappingsResponse merge(AtomicReferenceArray<Object> indexResponses) { Map<String, Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetaData>>> mergedResponses = new HashMap<>(); for (int i = 0; i < indexResponses.length(); i++) { Object element = indexResponses.get(i); if (element instanceof GetFieldMappingsResponse) { GetFieldMappingsResponse response = (GetFieldMappingsResponse) element; mergedResponses.putAll(response.mappings()); } } return new GetFieldMappingsResponse(unmodifiableMap(mergedResponses)); }
public void reapConnections() { long stale = System.currentTimeMillis() - TIME_OUT; int i = 0; while ((connections.length() != 0) && (connections.get(i) != null)) { AtomicReference<JDCConnection> conn = new AtomicReference<>(connections.get(i)); if ((conn.get().inUse()) && (stale > conn.get().getLastUse()) && (!conn.get().validate())) { removeConnection(i); } else { i++; } } }
/** * Obtains an instance of {@code DayOfMonth}. * * <p>A day-of-month object represents one of the 31 days of the month, from 1 to 31. * * @param dayOfMonth the day-of-month to represent, from 1 to 31 * @return the day-of-month, not null * @throws CalendricalException if the day-of-month is invalid */ public static DayOfMonth of(int dayOfMonth) { try { DayOfMonth result = CACHE.get(--dayOfMonth); if (result == null) { DayOfMonth temp = new DayOfMonth(dayOfMonth + 1); CACHE.compareAndSet(dayOfMonth, null, temp); result = CACHE.get(dayOfMonth); } return result; } catch (IndexOutOfBoundsException ex) { throw new CalendricalException("Invalid value for DayOfYear: " + ++dayOfMonth); } }
/** * Obtains an instance of <code>MinuteOfHour</code>. * * @param minuteOfHour the minute-of-hour to represent, from 0 to 59 * @return the created MinuteOfHour * @throws IllegalCalendarFieldValueException if the minuteOfHour is invalid */ public static MinuteOfHour minuteOfHour(int minuteOfHour) { try { MinuteOfHour result = CACHE.get(minuteOfHour); if (result == null) { MinuteOfHour temp = new MinuteOfHour(minuteOfHour); CACHE.compareAndSet(minuteOfHour, null, temp); result = CACHE.get(minuteOfHour); } return result; } catch (IndexOutOfBoundsException ex) { throw new IllegalCalendarFieldValueException(rule(), minuteOfHour); } }
@Override protected ReplicationPingResponse newResponseInstance( ReplicationPingRequest request, AtomicReferenceArray indexResponses) { ReplicationPingResponse response = new ReplicationPingResponse(); for (int i = 0; i < indexResponses.length(); i++) { IndexReplicationPingResponse indexResponse = (IndexReplicationPingResponse) indexResponses.get(i); if (indexResponse != null) { response.indices().put(indexResponse.index(), indexResponse); } } return response; }
/** * Obtains an instance of <code>WeekOfWeekBasedYear</code> from a value. * * <p>A week of week-based-year object represents one of the 53 weeks of the year, from 1 to 53. * These are cached internally and returned as singletons, so they can be compared using ==. * * @param weekOfWeekyear the week of week-based-year to represent, from 1 to 53 * @return the WeekOfWeekBasedYear singleton, never null * @throws IllegalCalendarFieldValueException if the weekOfWeekyear is invalid */ public static WeekOfWeekBasedYear weekOfWeekBasedYear(int weekOfWeekyear) { try { WeekOfWeekBasedYear result = CACHE.get(--weekOfWeekyear); if (result == null) { WeekOfWeekBasedYear temp = new WeekOfWeekBasedYear(weekOfWeekyear + 1); CACHE.compareAndSet(weekOfWeekyear, null, temp); result = CACHE.get(weekOfWeekyear); } return result; } catch (IndexOutOfBoundsException ex) { throw new IllegalCalendarFieldValueException(rule(), ++weekOfWeekyear); } }
protected void onNodeResponse(DiscoveryNode node, int nodeIndex, NodeResponse response) { logger.trace("received response for [{}] from node [{}]", actionName, node.id()); // this is defensive to protect against the possibility of double invocation // the current implementation of TransportService#sendRequest guards against this // but concurrency is hard, safety is important, and the small performance loss here does not // matter if (responses.compareAndSet(nodeIndex, null, response)) { if (counter.incrementAndGet() == responses.length()) { onCompletion(); } } }
private boolean addHashEntry( final AtomicReferenceArray<ClassInfoEntry> hashTable, final ClassInfo ci, final int hash) { ClassInfoEntry cie = hashTable.get(hash); while (cie != null) { if (ci.equals(cie._classInfo)) { return false; } cie = cie._next; } cie = hashTable.get(hash); final ClassInfoEntry newCie = new ClassInfoEntry(ci, cie); hashTable.set(hash, newCie); _size.incrementAndGet(); return true; }
private void addHashEntry( final AtomicReferenceArray<ClassInfoEntry> hashTable, final ClassInfo ci) { final int hh = ci.getHandle() % hashTable.length(); final int nh = ci.getDescribedClass() == null ? -1 : ((ci.getDescribedClass().getName().hashCode() & 0x7FFFFFFF) % hashTable.length()); boolean added = addHashEntry(hashTable, ci, hh); if (nh != -1 && nh != hh) { added |= addHashEntry(hashTable, ci, nh); } if (!added) { _discardedDuplicates.incrementAndGet(); } }
@Override protected ValidateQueryResponse newResponse( ValidateQueryRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) { int successfulShards = 0; int failedShards = 0; boolean valid = true; List<ShardOperationFailedException> shardFailures = null; List<QueryExplanation> queryExplanations = null; for (int i = 0; i < shardsResponses.length(); i++) { Object shardResponse = shardsResponses.get(i); if (shardResponse == null) { // simply ignore non active shards } else if (shardResponse instanceof BroadcastShardOperationFailedException) { failedShards++; if (shardFailures == null) { shardFailures = new ArrayList<>(); } shardFailures.add( new DefaultShardOperationFailedException( (BroadcastShardOperationFailedException) shardResponse)); } else { ShardValidateQueryResponse validateQueryResponse = (ShardValidateQueryResponse) shardResponse; valid = valid && validateQueryResponse.isValid(); if (request.explain() || request.rewrite()) { if (queryExplanations == null) { queryExplanations = new ArrayList<>(); } queryExplanations.add( new QueryExplanation( validateQueryResponse.getIndex(), validateQueryResponse.isValid(), validateQueryResponse.getExplanation(), validateQueryResponse.getError())); } successfulShards++; } } return new ValidateQueryResponse( valid, queryExplanations, shardsResponses.length(), successfulShards, failedShards, shardFailures); }
@SuppressWarnings({"unchecked"}) void onOperation(ShardRouting shard, int shardIndex, ShardResponse response) { shardsResponses.set(shardIndex, response); if (expectedOps == counterOps.incrementAndGet()) { finishHim(); } }
@Override protected ClusterStatsResponse newResponse( ClusterStatsRequest clusterStatsRequest, AtomicReferenceArray responses) { final List<ClusterStatsNodeResponse> nodeStats = new ArrayList<>(responses.length()); for (int i = 0; i < responses.length(); i++) { Object resp = responses.get(i); if (resp instanceof ClusterStatsNodeResponse) { nodeStats.add((ClusterStatsNodeResponse) resp); } } return new ClusterStatsResponse( System.currentTimeMillis(), clusterName, clusterService.state().metaData().clusterUUID(), nodeStats.toArray(new ClusterStatsNodeResponse[nodeStats.size()])); }