Exemplo n.º 1
0
  /**
   * This method calculates the minimum view ID known by the current node. This method is only used
   * in a clustered cache, and only invoked when either a view change is detected, or a transaction
   * whose view ID is not the same as the current view ID.
   *
   * <p>This method is guarded by minViewRecalculationLock to prevent concurrent updates to the
   * minimum view ID field.
   *
   * @param idOfRemovedTransaction the view ID associated with the transaction that triggered this
   *     recalculation, or -1 if triggered by a view change event.
   */
  @GuardedBy("minViewRecalculationLock")
  private void calculateMinViewId(int idOfRemovedTransaction) {
    minViewRecalculationLock.lock();
    try {
      // We should only need to re-calculate the minimum view ID if the transaction being completed
      // has the same ID as the smallest known transaction ID, to check what the new smallest is.
      // We do this check
      // again here, since this is now within a synchronized method.
      if (idOfRemovedTransaction == -1
          || (idOfRemovedTransaction == minTxViewId && idOfRemovedTransaction < currentViewId)) {
        int minViewIdFound = currentViewId;

        for (CacheTransaction ct : localTransactions.values()) {
          int viewId = ct.getViewId();
          if (viewId < minViewIdFound) minViewIdFound = viewId;
        }
        for (CacheTransaction ct : remoteTransactions.values()) {
          int viewId = ct.getViewId();
          if (viewId < minViewIdFound) minViewIdFound = viewId;
        }
        if (minViewIdFound > minTxViewId) {
          log.tracef("Changing minimum view ID from %s to %s", minTxViewId, minViewIdFound);
          minTxViewId = minViewIdFound;
        } else {
          log.tracef("Minimum view ID still is %s; nothing to change", minViewIdFound);
        }
      }
    } finally {
      minViewRecalculationLock.unlock();
    }
  }
  @Override
  public synchronized void stop() throws Exception {
    if (!started) {
      return;
    }

    // Channel may be null if there isn't a connection to a live server
    if (channel != null) {
      channel.close();
    }

    for (ReplicatedLargeMessage largeMessage : largeMessages.values()) {
      largeMessage.releaseResources();
    }
    largeMessages.clear();

    for (Entry<JournalContent, Map<Long, JournalSyncFile>> entry :
        filesReservedForSync.entrySet()) {
      for (JournalSyncFile filesReserved : entry.getValue().values()) {
        filesReserved.close();
      }
    }

    filesReservedForSync.clear();
    if (journals != null) {
      for (Journal j : journals) {
        if (j instanceof FileWrapperJournal) j.stop();
      }
    }

    for (ConcurrentMap<Integer, Page> map : pageIndex.values()) {
      for (Page page : map.values()) {
        try {
          page.sync();
          page.close();
        } catch (Exception e) {
          ActiveMQServerLogger.LOGGER.errorClosingPageOnReplication(e);
        }
      }
    }
    pageManager.stop();

    pageIndex.clear();
    final CountDownLatch latch = new CountDownLatch(1);
    executor.execute(
        new Runnable() {

          @Override
          public void run() {
            latch.countDown();
          }
        });
    latch.await(30, TimeUnit.SECONDS);

    // Storage needs to be the last to stop
    storageManager.stop();

    started = false;
  }
  @Override
  protected void closeListeners() {
    deregisterAllCacheEntryListener(syncListenerRegistrations.values());
    deregisterAllCacheEntryListener(asyncListenerRegistrations.values());

    syncListenerRegistrations.clear();
    asyncListenerRegistrations.clear();
    notifyAndClearSyncListenerLatches();
  }
Exemplo n.º 4
0
  /**
   * This method is public only so it can be invoked by unit testing, but should not otherwise be
   * used !
   */
  @ManagedOperation(
      description = "Trashes all connections to other nodes. This is only used for testing")
  public void removeAllConnections() {
    for (SenderEntry entry : send_table.values()) entry.reset();
    send_table.clear();

    sendStableMessages();
    for (ReceiverEntry entry2 : recv_table.values()) entry2.reset();
    recv_table.clear();
  }
Exemplo n.º 5
0
 @ManagedAttribute(description = "Number of purges in all (receive and send) windows")
 public int getXmitTableNumPurges() {
   int retval = 0;
   for (ReceiverEntry entry : recv_table.values()) {
     if (entry.received_msgs != null) retval += entry.received_msgs.getNumPurges();
   }
   for (SenderEntry entry : send_table.values()) {
     if (entry.sent_msgs != null) retval += entry.sent_msgs.getNumPurges();
   }
   return retval;
 }
 public void getSubRecordsBetween(MultiResultSet results, Long from, Long to) {
   Set<Long> values = sortedSet.subSet(from, to);
   for (Long value : values) {
     ConcurrentMap<Long, Record> records = mapRecords.get(value);
     if (records != null) {
       results.addResultSet(value, records.values());
     }
   }
   // to wasn't included so include now
   ConcurrentMap<Long, Record> records = mapRecords.get(to);
   if (records != null) {
     results.addResultSet(to, records.values());
   }
 }
Exemplo n.º 7
0
  /**
   * Removes a Resident from the Datasource
   *
   * @param resident
   */
  public boolean removeResident(Resident resident) {
    boolean result = residents.remove(resident.getUUID()) != null;

    for (Town t : towns.values()) if (t.hasResident(resident)) t.removeResident(resident);

    return result;
  }
Exemplo n.º 8
0
  /* ------------------------------------------------------------ */
  private void shrinkCache() {
    // While we need to shrink
    while (_cache.size() > 0
        && (_cachedFiles.get() > _maxCachedFiles || _cachedSize.get() > _maxCacheSize)) {
      // Scan the entire cache and generate an ordered list by last accessed time.
      SortedSet<Content> sorted =
          new TreeSet<Content>(
              new Comparator<Content>() {
                public int compare(Content c1, Content c2) {
                  if (c1._lastAccessed < c2._lastAccessed) return -1;

                  if (c1._lastAccessed > c2._lastAccessed) return 1;

                  if (c1._length < c2._length) return -1;

                  return c1._key.compareTo(c2._key);
                }
              });
      for (Content content : _cache.values()) sorted.add(content);

      // Invalidate least recently used first
      for (Content content : sorted) {
        if (_cachedFiles.get() <= _maxCachedFiles && _cachedSize.get() <= _maxCacheSize) break;
        if (content == _cache.remove(content.getKey())) content.invalidate();
      }
    }
  }
Exemplo n.º 9
0
  public synchronized void stop() {
    if (!started) {
      return;
    }

    if (!paused) {
      InVMRegistry.instance.unregisterAcceptor(id);
    }

    for (Connection connection : connections.values()) {
      listener.connectionDestroyed(connection.getID());
    }

    connections.clear();

    if (notificationService != null) {
      TypedProperties props = new TypedProperties();
      props.putSimpleStringProperty(
          new SimpleString("factory"), new SimpleString(InVMAcceptorFactory.class.getName()));
      props.putIntProperty(new SimpleString("id"), id);
      Notification notification = new Notification(null, NotificationType.ACCEPTOR_STOPPED, props);
      try {
        notificationService.sendNotification(notification);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    started = false;

    paused = false;
  }
Exemplo n.º 10
0
  public void reset() {
    blobOpenCount.set(0);
    clobOpenCount.set(0);

    connectionStat.reset();
    statementStat.reset();
    resultSetStat.reset();
    connectionHoldHistogram.reset();

    lock.writeLock().lock();
    try {
      Iterator<Map.Entry<String, JdbcSqlStat>> iter = sqlStatMap.entrySet().iterator();
      while (iter.hasNext()) {
        Map.Entry<String, JdbcSqlStat> entry = iter.next();
        JdbcSqlStat stat = entry.getValue();
        if (stat.getExecuteCount() == 0 && stat.getRunningCount() == 0) {
          stat.setRemoved(true);
          iter.remove();
        } else {
          stat.reset();
        }
      }
    } finally {
      lock.writeLock().unlock();
    }

    for (JdbcConnectionStat.Entry connectionStat : connections.values()) {
      connectionStat.reset();
    }
  }
Exemplo n.º 11
0
  private void multicastJoin(int count, final boolean sleep) throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(count);

    final Config config = new Config();
    config.setProperty("hazelcast.wait.seconds.before.join", "5");
    config.getNetworkConfig().getJoin().getMulticastConfig().setMulticastTimeoutSeconds(25);
    final ConcurrentMap<Integer, HazelcastInstance> map =
        new ConcurrentHashMap<Integer, HazelcastInstance>();
    final CountDownLatch latch = new CountDownLatch(count);
    final ExecutorService ex = Executors.newCachedThreadPool();
    for (int i = 0; i < count; i++) {
      final int index = i;
      ex.execute(
          new Runnable() {
            public void run() {
              if (sleep) {
                try {
                  Thread.sleep((int) (1000 * Math.random()));
                } catch (InterruptedException ignored) {
                }
              }
              HazelcastInstance h = nodeFactory.newHazelcastInstance(config);
              map.put(index, h);
              latch.countDown();
            }
          });
    }
    assertOpenEventually(latch);
    for (HazelcastInstance h : map.values()) {
      assertEquals(count, h.getCluster().getMembers().size());
    }
    ex.shutdown();
  }
Exemplo n.º 12
0
  /**
   * @param cancel {@code True} to close with cancellation.
   * @throws GridException If failed.
   */
  @Override
  public void close(boolean cancel) throws GridException {
    if (!closed.compareAndSet(false, true)) return;

    busyLock.block();

    if (log.isDebugEnabled())
      log.debug("Closing data loader [ldr=" + this + ", cancel=" + cancel + ']');

    GridException e = null;

    try {
      // Assuming that no methods are called on this loader after this method is called.
      if (cancel) {
        cancelled = true;

        for (Buffer buf : bufMappings.values()) buf.cancelAll();
      } else doFlush();

      ctx.event().removeLocalEventListener(discoLsnr);

      ctx.io().removeMessageListener(topic);
    } catch (GridException e0) {
      e = e0;
    }

    fut.onDone(null, e);

    if (e != null) throw e;
  }
Exemplo n.º 13
0
 public Collection<? extends DistributedObject> getDistributedObjects() {
   Collection<DistributedObject> objects = new LinkedList<DistributedObject>();
   for (ClientProxyFuture future : proxies.values()) {
     objects.add(future.get());
   }
   return objects;
 }
Exemplo n.º 14
0
  private ListenableFuture<Duration> announce() {
    final ListenableFuture<Duration> future =
        announcementClient.announce(ImmutableSet.copyOf(announcements.values()));

    Futures.addCallback(
        future,
        new FutureCallback<Duration>() {
          @Override
          public void onSuccess(Duration duration) {
            errorBackOff.success();

            // wait 80% of the suggested delay
            duration = new Duration(duration.toMillis() * 0.8, MILLISECONDS);
            scheduleNextAnnouncement(duration);
          }

          @Override
          public void onFailure(Throwable t) {
            Duration duration = errorBackOff.failed(t);
            scheduleNextAnnouncement(duration);
          }
        },
        executor);

    return future;
  }
Exemplo n.º 15
0
  public Object up(Event evt) {
    switch (evt.getType()) {
      case Event.MSG:
        Message msg = (Message) evt.getArg();
        ForkHeader hdr = (ForkHeader) msg.getHeader(id);
        if (hdr == null) break;
        if (hdr.fork_stack_id == null)
          throw new IllegalArgumentException("header has a null fork_stack_id");
        Protocol bottom_prot = get(hdr.fork_stack_id);
        return bottom_prot != null
            ? bottom_prot.up(evt)
            : this.unknownForkHandler.handleUnknownForkStack(msg, hdr.fork_stack_id);

      case Event.VIEW_CHANGE:
        for (Protocol bottom : fork_stacks.values()) bottom.up(evt);
        break;

      case Event.STATE_TRANSFER_OUTPUTSTREAM:
        if (!process_state_events) break;
        getStateFromMainAndForkChannels(evt);
        return null;

      case Event.STATE_TRANSFER_INPUTSTREAM:
        if (!process_state_events) break;
        setStateInMainAndForkChannels((InputStream) evt.getArg());
        return null;
    }
    return up_prot.up(evt);
  }
Exemplo n.º 16
0
  /** Remove completed queries after a waiting period */
  public void removeExpiredQueries() {
    List<QueryExecution> sortedQueries =
        IterableTransformer.on(queries.values())
            .select(compose(not(isNull()), endTimeGetter()))
            .orderBy(Ordering.natural().onResultOf(endTimeGetter()))
            .list();

    int toRemove = Math.max(sortedQueries.size() - maxQueryHistory, 0);
    DateTime oldestAllowedQuery = DateTime.now().minus(maxQueryAge.toMillis());

    for (QueryExecution queryExecution : sortedQueries) {
      try {
        DateTime endTime = queryExecution.getQueryInfo().getQueryStats().getEndTime();
        if ((endTime.isBefore(oldestAllowedQuery) || toRemove > 0) && isAbandoned(queryExecution)) {
          removeQuery(queryExecution.getQueryInfo().getQueryId());
          --toRemove;
        }
      } catch (RuntimeException e) {
        log.warn(
            e,
            "Error while inspecting age of query %s",
            queryExecution.getQueryInfo().getQueryId());
      }
    }
  }
Exemplo n.º 17
0
  /** Checks for stale records and removes them. */
  private void reapStaleRecords(long currentTime) {
    LinkedList<AdvertisementInfo> toRemove = new LinkedList<AdvertisementInfo>();

    synchronized (_clientLock) {
      for (AdvertisementInfo adInfo : _advertisements.values()) {
        long timeDiff = (currentTime / 1000000) - adInfo.timeStamp;

        if (timeDiff > STALE_TIME) toRemove.add(adInfo);
      }

      // reap if necessary
      if (toRemove.size() > 0) {
        StringBuilder sb = new StringBuilder();

        for (AdvertisementInfo adInfo : toRemove) {
          if (sb.length() > 0) sb.append(",");

          _advertisements.remove(adInfo.name);
          sb.append(adInfo.name);
        }

        _logger.info(
            "{} stale record{} removed. [{}]",
            toRemove.size(),
            toRemove.size() == 1 ? " was" : "s were",
            sb.toString());
      }
    }
  } // (method)
 @Override
 public void notify(NodeTypes updatedNodeTypes) {
   // Notify all of the providers about the change in node types ...
   for (IndexProvider provider : providers.values()) {
     provider.notify(updatedNodeTypes);
   }
 }
Exemplo n.º 19
0
  @Override
  public Object create(String path)
      throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
    Class<?> clazz = serviceCache.get(path);
    if (clazz == null) {
      StringBuffer warnings = new StringBuffer();
      // We need to look for a bundle that has that class.
      int wrrningCounter = 1;
      for (BundleWrapper wrapper : bundleWrappers.values()) {
        URL resource = wrapper.bundle.getResource(path);
        if (resource == null) {
          continue;
        }

        Properties properties = loadProperties(resource);

        String className = properties.getProperty("class");
        if (className == null) {
          warnings.append(
              "("
                  + (wrrningCounter++)
                  + ") Invalid service file in bundle "
                  + wrapper
                  + ": 'class' property not defined.");
          continue;
        }

        try {
          clazz = wrapper.bundle.loadClass(className);
        } catch (ClassNotFoundException e) {
          warnings.append(
              "("
                  + (wrrningCounter++)
                  + ") Bundle "
                  + wrapper
                  + " could not load "
                  + className
                  + ": "
                  + e);
          continue;
        }

        // Yay.. the class was found.  Now cache it.
        serviceCache.put(path, clazz);
        wrapper.cachedServices.add(path);
        break;
      }

      if (clazz == null) {
        // Since OSGi is such a tricky environment to work in.. lets give folks the
        // most information we can in the error message.
        String msg = "Service not found: '" + path + "'";
        if (warnings.length() != 0) {
          msg += ", " + warnings;
        }
        throw new IOException(msg);
      }
    }
    return clazz.newInstance();
  }
  /**
   * Closes all opened connections.
   *
   * @param waitCompletion If {@code true} waits for all pending requests to be proceeded.
   */
  @SuppressWarnings("TooBroadScope")
  @Override
  public void stop(boolean waitCompletion) {
    Collection<GridClientConnection> closeConns;

    if (closed) return;

    // Mark manager as closed.
    closed = true;

    // Remove all connections from cache.
    closeConns = new ArrayList<>(conns.values());

    conns.clear();

    nodeConns.clear();

    // Close old connection outside the writer lock.
    for (GridClientConnection conn : closeConns) conn.close(CLIENT_CLOSED, waitCompletion);

    if (pingExecutor != null)
      GridClientUtils.shutdownNow(GridClientConnectionManager.class, pingExecutor, log);

    GridClientUtils.shutdownNow(GridClientConnectionManager.class, executor, log);

    if (srv != null) srv.stop();
  }
Exemplo n.º 21
0
  @Override
  public Set<Map.Entry<Data, Data>> entrySetData() {
    checkIfLoaded();
    final long now = getNow();

    final ConcurrentMap<Data, Record> records = this.records;
    final Collection<Record> values = records.values();
    Map<Data, Data> tempMap = null;
    for (Record record : values) {
      record = getOrNullIfExpired(record, now, false);
      if (record == null) {
        continue;
      }
      if (tempMap == null) {
        tempMap = new HashMap<Data, Data>();
      }
      final Data key = record.getKey();
      final Data value = toData(record.getValue());
      tempMap.put(key, value);
    }
    if (tempMap == null) {
      return Collections.emptySet();
    }
    return tempMap.entrySet();
  }
Exemplo n.º 22
0
  @Override
  public Collection<Data> valuesData() {
    checkIfLoaded();
    final long now = getNow();

    final ConcurrentMap<Data, Record> records = this.records;
    final Collection<Record> values = records.values();
    List<Data> dataValueList = null;
    for (Record record : values) {
      record = getOrNullIfExpired(record, now, false);
      if (record == null) {
        continue;
      }
      if (dataValueList == null) {
        dataValueList = new ArrayList<Data>();
      }
      final Data dataValue = toData(record.getValue());
      dataValueList.add(dataValue);
    }

    if (dataValueList == null) {
      return Collections.emptyList();
    }
    return dataValueList;
  }
Exemplo n.º 23
0
 /**
  * Refresh the includes/excludes information.
  *
  * @throws IOException
  */
 public synchronized void refreshNodes() throws IOException {
   hostsReader.refresh();
   LOG.info(
       "After refresh Included hosts: "
           + hostsReader.getHostNames().size()
           + " Excluded hosts: "
           + hostsReader.getExcludedHosts().size());
   Set<String> newHosts = hostsReader.getHostNames();
   Set<String> newExcludes = hostsReader.getExcludedHosts();
   Set<ClusterNode> hostsToExclude = new HashSet<ClusterNode>();
   for (ClusterNode tmpNode : nameToNode.values()) {
     String host = tmpNode.getHost();
     // Check if not included or explicitly excluded.
     if (!newHosts.contains(host) || newExcludes.contains(host)) {
       hostsToExclude.add(tmpNode);
     }
   }
   for (ClusterNode node : hostsToExclude) {
     synchronized (node) {
       for (Map.Entry<ResourceType, RunnableIndices> entry : typeToIndices.entrySet()) {
         ResourceType type = entry.getKey();
         RunnableIndices r = entry.getValue();
         if (r.hasRunnable(node)) {
           LOG.info(
               "Node "
                   + node.getName()
                   + " is no longer "
                   + type
                   + " runnable because it is excluded");
           r.deleteRunnable(node);
         }
       }
     }
   }
 }
Exemplo n.º 24
0
  @Override
  public synchronized void close() throws SQLException {
    try {
      closeLock.writeLock().lock();
      if (closed) {
        return;
      }
      closed = true;
    } finally {
      closeLock.writeLock().unlock();
    }

    try {
      Collection<ConnectionQueryServices> connectionQueryServices =
          connectionQueryServicesMap.values();
      try {
        SQLCloseables.closeAll(connectionQueryServices);
      } finally {
        connectionQueryServices.clear();
      }
    } finally {
      if (services != null) {
        try {
          services.close();
        } finally {
          ExecutorService executor = services.getExecutor();
          // Even if something wrong happened while closing services above, we still
          // want to set it to null. Otherwise, we will end up having a possibly non-working
          // services instance.
          services = null;
          executor.shutdown();
        }
      }
    }
  }
Exemplo n.º 25
0
 /**
  * Gets all the consumer endpoints.
  *
  * @return consumer endpoints
  */
 public static Collection<Endpoint> getConsumerEndpoints() {
   Collection<Endpoint> endpoints = new ArrayList<Endpoint>(CONSUMERS.size());
   for (DirectVmConsumer consumer : CONSUMERS.values()) {
     endpoints.add(consumer.getEndpoint());
   }
   return endpoints;
 }
Exemplo n.º 26
0
 public Collection<V> values() {
   Collection<V> copied = new ArrayList<>();
   for (V v : map.values()) {
     copied.add(Checker.copyIfRequired(v));
   }
   return copied;
 }
  // GET /predictions2
  // GET /predictions2?id=1
  // If the HTTP Accept header is set to application/json (or an equivalent
  // such as text/x-json), the response is JSON and XML otherwise.
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    String param = request.getParameter("id");
    Integer key = (param == null) ? null : new Integer(param.trim());

    // Check user preference for XML or JSON by inspecting
    // the HTTP headers for the Accept key.
    boolean json = false;
    String accept = request.getHeader("accept");

    if (accept != null && accept.contains("json")) json = true;

    // If no query string, assume client wants the full list.
    if (key == null) {
      ConcurrentMap<Integer, Prediction> map = predictions.getPredictions();

      // Sort the map's values for readability.
      Object[] list = map.values().toArray();
      Arrays.sort(list);
      String xml = predictions.toXML(list);
      sendResponse(response, xml, json);
    }

    // Otherwise, return the specified Prediction.
    else {
      Prediction prediction = predictions.getPredictions().get(key);

      if (prediction == null) { // no such prediction
        String msg = key + " does not map to a prediction.\n";
        sendResponse(response, predictions.toXML(msg), false);
      } else { // requested Prediction found
        sendResponse(response, predictions.toXML(prediction), json);
      }
    }
  }
 @Override
 public void markInvalidated() {
   for (PsiFileImpl file : myRoots.values()) {
     file.markInvalidated();
   }
   super.markInvalidated();
 }
Exemplo n.º 29
0
 public void destroy() {
   super.destroy();
   try {
     expireFuture.cancel(true);
   } catch (Throwable t) {
     logger.warn(t.getMessage(), t);
   }
   try {
     for (RedisRegistryNotifier notifier : notifiers.values()) {
       notifier.shutdown();
     }
   } catch (Throwable t) {
     logger.warn(t.getMessage(), t);
   }
   for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
     JedisPool jedisPool = entry.getValue();
     try {
       jedisPool.destroy();
     } catch (Throwable t) {
       logger.warn(
           "Failed to destroy the redis registry client. registry: "
               + entry.getKey()
               + ", cause: "
               + t.getMessage(),
           t);
     }
   }
 }
Exemplo n.º 30
0
        @Override
        public void run() {

          try {
            try {
              List<Transaction> transactionList = new ArrayList<>();
              int curTime = Convert.getEpochTime();
              for (TransactionImpl transaction : nonBroadcastedTransactions.values()) {
                if (TransactionDb.hasTransaction(transaction.getId())
                    || transaction.getExpiration() < curTime) {
                  nonBroadcastedTransactions.remove(transaction.getId());
                } else if (transaction.getTimestamp() < curTime - 30) {
                  transactionList.add(transaction);
                }
              }

              if (transactionList.size() > 0) {
                Peers.sendToSomePeers(transactionList);
              }

            } catch (Exception e) {
              Logger.logDebugMessage("Error in transaction re-broadcasting thread", e);
            }
          } catch (Throwable t) {
            Logger.logMessage("CRITICAL ERROR. PLEASE REPORT TO THE DEVELOPERS.\n" + t.toString());
            t.printStackTrace();
            System.exit(1);
          }
        }