/** Cleans up resources to avoid excessive memory usage. */
  public void cleanUp() {
    topSnapshot.set(null);
    singleMsgs.clear();
    fullMsgs.clear();
    rcvdIds.clear();
    oldestNode.set(null);
    partReleaseFut = null;

    Collection<ClusterNode> rmtNodes = this.rmtNodes;

    if (rmtNodes != null) rmtNodes.clear();
  }
  /** @throws Exception Thrown if test failed. */
  public void testA() throws Exception {
    Collection<Integer> set = new GridConcurrentWeakHashSet<>();

    Integer i = 1;

    assert set.add(i);
    assert !set.add(i);

    assert set.contains(i);

    assert set.size() == 1;

    Collection<Integer> c = F.asList(2, 3, 4, 5);

    assert set.addAll(c);
    assert !set.addAll(c);

    assert set.containsAll(c);

    assert set.size() == 1 + c.size();

    assert set.remove(i);
    assert !set.remove(i);

    assert !set.contains(i);

    assert set.size() == c.size();

    assert set.removeAll(c);
    assert !set.removeAll(c);

    assert !set.containsAll(c);

    assert set.isEmpty();

    Collection<Integer> c1 = Arrays.asList(1, 3, 5, 7, 9);

    int cnt = 0;

    for (Iterator<Integer> iter = set.iterator(); iter.hasNext(); cnt++) c1.contains(iter.next());

    assert set.size() == cnt;

    assert set.size() == set.toArray().length;

    assert set.addAll(c1);

    assert set.retainAll(c);
    assert !set.retainAll(c);

    Collection<Integer> c2 = F.retain(c1, true, c);

    assert set.containsAll(c2);
    assert !set.containsAll(c1);
    assert !set.containsAll(c);

    assert set.size() == c2.size();

    set.clear();

    assert set.isEmpty();

    try {
      set.iterator().next();

      assert false;
    } catch (NoSuchElementException ignored) {
      assert true;
    }

    try {
      set.add(null);

      assert false;
    } catch (NullPointerException ignored) {
      assert true;
    }
  }
Exemplo n.º 3
0
  /**
   * Checks all known service infos for matches to the given class, returns all matching entries.
   *
   * @param plugin
   * @param options
   * @return
   */
  @SuppressWarnings("boxing")
  private Collection<DiscoveredPlugin> resolve(
      final Class<? extends Plugin> plugin, final DiscoverOption[] options) {

    // Refreshes all known remote discovery managers.
    refreshKnownDiscoveryManagers();

    // The result we intend to return
    final Collection<DiscoveredPlugin> result = new ArrayList<DiscoveredPlugin>();
    final Collection<RemoteManagerEndpoint> endpoints = this.remoteManagersEndpoints;

    // Query all managers
    for (final RemoteManagerEndpoint endpoint : endpoints) {

      // The manager to use ...
      final DiscoveryManager mgr =
          getRemoteProxyToDiscoveryManager(endpoint.address.getHostAddress(), endpoint.port);

      // No matter what happens, close the manager
      try {
        if (mgr == null) {
          this.logger.status(
              "resolve/vanished",
              "address",
              endpoint.address.getHostAddress(),
              "port",
              endpoint.port);
          continue;
        }

        //
        final AtomicInteger pingTime = new AtomicInteger(Integer.MAX_VALUE);

        // Get ping time
        AccessController.doPrivileged(
            new PrivilegedAction<Object>() {
              public Object run() {
                try {
                  final long start = System.nanoTime();

                  // Perform the ping
                  mgr.ping(new Random().nextInt());

                  // Obtain the time
                  final long stop = System.nanoTime();
                  final long time = (stop - start) / 1000;

                  // And set it
                  NetworkProbe.this.logger.status("resolve/ping", "time", time);
                  pingTime.set((int) time);
                } catch (final Exception e) {
                  NetworkProbe.this.logger.status(
                      "resolve/exception/ping", "message", e.getMessage());
                }
                return null;
              }
            });

        // Query the information (needs to be inside a privileged block, otherwise applets might
        // complain.
        final ExportInfo exportInfo =
            AccessController.doPrivileged(
                new PrivilegedAction<ExportInfo>() {
                  public ExportInfo run() {
                    return mgr.getExportInfoFor(plugin.getCanonicalName());
                  }
                });

        // If the plugin in not exported, do nothing
        if (!exportInfo.isExported) {
          continue;
        }

        // If it is, construct required data.
        for (final ExportedPlugin p : exportInfo.allExported) {
          final PublishMethod method = PublishMethod.valueOf(p.exportMethod);
          final URI uri = p.exportURI;

          String _newURI = "";

          _newURI += uri.getScheme();
          _newURI += "://";

          if (endpoint.address != null) {
            _newURI += endpoint.address.getHostAddress();
          } else {
            _newURI += "127.0.0.1";
          }
          _newURI += ":";
          _newURI += uri.getPort();
          _newURI += uri.getPath();

          try {
            // TODO: Compute distance properly.
            result.add(
                new DiscoveredPluginImpl(
                    method, new URI(_newURI), pingTime.get(), p.timeSinceExport));
          } catch (final URISyntaxException e) {
            e.printStackTrace();
          }
        }
      } catch (final Exception e) {
        this.logger.status(
            "resolve/exception/versionmess", "address", endpoint.address, "port", endpoint.port);
        e.printStackTrace();
      } finally {
        // In case the manager is of the type simple resource (which it should be), try to close it.
        if (mgr instanceof SimpleResource) {
          try {

            // Try to close our device again
            AccessController.doPrivileged(
                new PrivilegedAction<Object>() {
                  public Object run() {
                    ((SimpleResource) mgr).close();
                    return null;
                  }
                });

          } catch (final Exception e) {
            e.printStackTrace();
            this.logger.status(
                "resolve/exception/close", "address", endpoint.address, "port", endpoint.port);
          }
        }
      }
    }

    // If we have no result there is nothing to do
    if (result.size() == 0) return result;

    // Prepapare filter options ...
    final OptionUtils<DiscoverOption> ou = new OptionUtils<DiscoverOption>(options);

    DiscoveredPlugin best = result.iterator().next();

    if (ou.contains(OptionNearest.class)) {
      // Check all plugins
      for (final DiscoveredPlugin p : result) {
        // If this one is closer, replace them
        if (p.getDistance() < best.getDistance()) {
          best = p;
        }
      }

      // Remove all other plugins
      result.clear();
      result.add(best);
    }

    if (ou.contains(OptionYoungest.class)) {
      // Check all plugins
      for (final DiscoveredPlugin p : result) {
        // If this one is closer, replace them
        if (p.getTimeSinceExport() < best.getTimeSinceExport()) {
          best = p;
        }
      }

      // Remove all other plugins
      result.clear();
      result.add(best);
    }

    if (ou.contains(OptionOldest.class)) {
      // Check all plugins
      for (final DiscoveredPlugin p : result) {
        // If this one is closer, replace them
        if (p.getTimeSinceExport() > best.getTimeSinceExport()) {
          best = p;
        }
      }

      // Remove all other plugins
      result.clear();
      result.add(best);
    }

    // Debug plugins
    for (final DiscoveredPlugin p : result) {
      this.logger.status("resolve/return/", "plugin", p.getPublishURI());
    }

    return result;
  }