コード例 #1
0
  /**
   * @param pid PID of the other party.
   * @param size Size of the space.
   * @return Token pair.
   */
  private IgnitePair<String> inOutToken(int pid, int size) {
    while (true) {
      long idx = tokIdxGen.get();

      if (tokIdxGen.compareAndSet(idx, idx + 2))
        return F.pair(
            new File(tokDir, TOKEN_FILE_NAME + idx + "-" + pid + "-" + size).getAbsolutePath(),
            new File(tokDir, TOKEN_FILE_NAME + (idx + 1) + "-" + pid + "-" + size)
                .getAbsolutePath());
    }
  }
コード例 #2
0
  /** Should request -1 for infinite */
  @Test
  public void testRequestFromFinalSubscribeWithoutRequestValue() {
    TestSubscriber<String> s = new TestSubscriber<>();
    final AtomicLong r = new AtomicLong();
    s.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(Long.MAX_VALUE, r.get());
  }
コード例 #3
0
ファイル: Distributor.java プロジェクト: superyfwy/db4o
  private Distributor(ByteArrayConsumer consumer, T root, Class<T> rootFacade) {
    addDefaultSerializers();
    setConsumer(consumer);

    if (root == null) {
      rootClient = proxyFor(objectsId.getAndIncrement(), rootFacade);
    } else {
      serverFor(root);
    }
  }
コード例 #4
0
  @Test
  public void testRequestFromChainedOperator() {
    TestSubscriber<String> s = new TestSubscriber<>();
    Operator<String, String> o =
        s1 ->
            new Subscriber<String>() {

              @Override
              public void onSubscribe(Subscription a) {
                s1.onSubscribe(a);
              }

              @Override
              public void onComplete() {}

              @Override
              public void onError(Throwable e) {}

              @Override
              public void onNext(String t) {}
            };
    s.request(10);
    Subscriber<? super String> ns = o.apply(s);

    final AtomicLong r = new AtomicLong();
    // set set the producer at the top of the chain (ns) and it should flow through the operator to
    // the (s) subscriber
    // and then it should request up with the value set on the final Subscriber (s)
    ns.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(10, r.get());
  }
コード例 #5
0
  @Test
  public void testRequestToObservable() {
    TestSubscriber<Integer> ts = new TestSubscriber<>();
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .subscribe(ts);
    assertEquals(3, requested.get());
  }
コード例 #6
0
  @Test
  public void testRequestThroughTakeWhereRequestIsSmallerThanTake() {
    TestSubscriber<Integer> ts = new TestSubscriber<>((Long) null);
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .take(10)
        .subscribe(ts);
    assertEquals(3, requested.get());
  }
コード例 #7
0
ファイル: Distributor.java プロジェクト: superyfwy/db4o
  protected Request request(long objectId, Method method, Object[] args, boolean expectsResponse)
      throws IOException {
    Request r = new Request(this, method, args);
    long requestId = -1;

    if (expectsResponse) {
      requestId = nextRequest.getAndIncrement();
      requests.put(requestId, r);
    }

    sendRequest(objectId, requestId, r);

    return r;
  }
コード例 #8
0
ファイル: Distributor.java プロジェクト: superyfwy/db4o
  public ServerObject serverFor(Object o) {

    ServerObject server = serving.get(o);

    if (server == null) {

      server = new ServerObject(objectsId.getAndIncrement(), o);

      serving.put(o, server);
      servingById.put(server.getId(), server);
    }

    return server;
  }
コード例 #9
0
  @Test
  public void testRequestThroughTakeThatReducesRequest() {
    TestSubscriber<Integer> ts = new TestSubscriber<>((Long) null);
    ts.request(3);
    final AtomicLong requested = new AtomicLong();
    Observable.<Integer>create(
            s ->
                s.onSubscribe(
                    new Subscription() {

                      @Override
                      public void request(long n) {
                        requested.set(n);
                      }

                      @Override
                      public void cancel() {}
                    }))
        .take(2)
        .subscribe(ts);

    // FIXME the take now requests Long.MAX_PATH if downstream requests at least the limit
    assertEquals(Long.MAX_VALUE, requested.get());
  }
コード例 #10
0
ファイル: Distributor.java プロジェクト: superyfwy/db4o
  private void processResponse(DataInputStream in) throws IOException {
    long requestId = in.readLong();
    Request r = requests.remove(requestId);

    if (r == null) {
      throw new IllegalStateException(
          "Request " + requestId + " is unknown (last request generated was " + nextRequest.get());
    }

    Object o = null;
    if (in.readBoolean()) {
      o = serializerFor(classForName(in.readUTF()), r.getResultDeclaredType()).deserialize(in);
    }
    r.set(o);
  }
コード例 #11
0
  /**
   * @param parent Parent entry.
   * @param nodeId Requesting node ID.
   * @param otherNodeId Near node ID.
   * @param otherVer Other version.
   * @param threadId Requesting thread ID.
   * @param ver Cache version.
   * @param timeout Maximum wait time.
   * @param loc {@code True} if the lock is local.
   * @param reentry {@code True} if candidate is for reentry.
   * @param tx Transaction flag.
   * @param singleImplicit Single-key-implicit-transaction flag.
   * @param nearLoc Near-local flag.
   * @param dhtLoc DHT local flag.
   */
  public GridCacheMvccCandidate(
      GridCacheEntryEx<K, ?> parent,
      UUID nodeId,
      @Nullable UUID otherNodeId,
      @Nullable GridCacheVersion otherVer,
      long threadId,
      GridCacheVersion ver,
      long timeout,
      boolean loc,
      boolean reentry,
      boolean tx,
      boolean singleImplicit,
      boolean nearLoc,
      boolean dhtLoc) {
    assert nodeId != null;
    assert ver != null;
    assert parent != null;

    this.parent = parent;
    this.nodeId = nodeId;
    this.otherNodeId = otherNodeId;
    this.otherVer = otherVer;
    this.threadId = threadId;
    this.ver = ver;
    this.timeout = timeout;

    mask(LOCAL, loc);
    mask(REENTRY, reentry);
    mask(TX, tx);
    mask(SINGLE_IMPLICIT, singleImplicit);
    mask(NEAR_LOCAL, nearLoc);
    mask(DHT_LOCAL, dhtLoc);

    ts = U.currentTimeMillis();

    id = IDGEN.incrementAndGet();
  }
コード例 #12
0
ファイル: Functional.java プロジェクト: zpublic/dozy
 public AtomicLong combine(AtomicLong x, AtomicLong y) {
   // Not clear whether this is meaningful:
   return new AtomicLong(x.addAndGet(y.get()));
 }
コード例 #13
0
    /**
     * @param entries Entries to submit.
     * @param curFut Current future.
     * @throws GridInterruptedException If interrupted.
     */
    private void submit(final List<Map.Entry<K, V>> entries, final GridFutureAdapter<Object> curFut)
        throws GridInterruptedException {
      assert entries != null;
      assert !entries.isEmpty();
      assert curFut != null;

      incrementActiveTasks();

      GridFuture<Object> fut;
      if (isLocNode) {
        fut =
            ctx.closure()
                .callLocalSafe(
                    new GridDataLoadUpdateJob<>(ctx, log, cacheName, entries, false, updater),
                    false);

        locFuts.add(fut);

        fut.listenAsync(
            new GridInClosure<GridFuture<Object>>() {
              @Override
              public void apply(GridFuture<Object> t) {
                try {
                  boolean rmv = locFuts.remove(t);

                  assert rmv;

                  curFut.onDone(t.get());
                } catch (GridException e) {
                  curFut.onDone(e);
                }
              }
            });
      } else {
        byte[] entriesBytes;

        try {
          entriesBytes = ctx.config().getMarshaller().marshal(entries);

          if (updaterBytes == null) {
            assert updater != null;

            updaterBytes = ctx.config().getMarshaller().marshal(updater);
          }

          if (topicBytes == null) topicBytes = ctx.config().getMarshaller().marshal(topic);
        } catch (GridException e) {
          U.error(log, "Failed to marshal (request will not be sent).", e);

          return;
        }

        GridDeployment dep = null;
        GridPeerDeployAware jobPda0 = null;

        if (ctx.deploy().enabled()) {
          try {
            jobPda0 = jobPda;

            assert jobPda0 != null;

            dep = ctx.deploy().deploy(jobPda0.deployClass(), jobPda0.classLoader());
          } catch (GridException e) {
            U.error(
                log,
                "Failed to deploy class (request will not be sent): " + jobPda0.deployClass(),
                e);

            return;
          }

          if (dep == null)
            U.warn(log, "Failed to deploy class (request will be sent): " + jobPda0.deployClass());
        }

        long reqId = idGen.incrementAndGet();

        fut = curFut;

        reqs.put(reqId, (GridFutureAdapter<Object>) fut);

        GridDataLoadRequest<Object, Object> req =
            new GridDataLoadRequest<>(
                reqId,
                topicBytes,
                cacheName,
                updaterBytes,
                entriesBytes,
                true,
                dep != null ? dep.deployMode() : null,
                dep != null ? jobPda0.deployClass().getName() : null,
                dep != null ? dep.userVersion() : null,
                dep != null ? dep.participants() : null,
                dep != null ? dep.classLoaderId() : null,
                dep == null);

        try {
          ctx.io().send(node, TOPIC_DATALOAD, req, PUBLIC_POOL);

          if (log.isDebugEnabled())
            log.debug("Sent request to node [nodeId=" + node.id() + ", req=" + req + ']');
        } catch (GridException e) {
          if (ctx.discovery().alive(node) && ctx.discovery().pingNode(node.id()))
            ((GridFutureAdapter<Object>) fut).onDone(e);
          else
            ((GridFutureAdapter<Object>) fut)
                .onDone(
                    new GridTopologyException(
                        "Failed to send " + "request (node has left): " + node.id()));
        }
      }
    }
コード例 #14
0
  @Test
  public void testRequestFromDecoupledOperatorThatRequestsN() {
    TestSubscriber<String> s = new TestSubscriber<>();
    final AtomicLong innerR = new AtomicLong();
    Operator<String, String> o =
        child -> {
          // we want to decouple the chain so set our own Producer on the child instead of it coming
          // from the parent
          child.onSubscribe(
              new Subscription() {

                @Override
                public void request(long n) {
                  innerR.set(n);
                }

                @Override
                public void cancel() {}
              });

          AsyncObserver<String> as =
              new AsyncObserver<String>() {

                @Override
                protected void onStart() {
                  // we request 99 up to the parent
                  request(99);
                }

                @Override
                public void onComplete() {}

                @Override
                public void onError(Throwable e) {}

                @Override
                public void onNext(String t) {}
              };
          return as;
        };
    s.request(10);
    Subscriber<? super String> ns = o.apply(s);

    final AtomicLong r = new AtomicLong();
    // set set the producer at the top of the chain (ns) and it should flow through the operator to
    // the (s) subscriber
    // and then it should request up with the value set on the final Subscriber (s)
    ns.onSubscribe(
        new Subscription() {

          @Override
          public void request(long n) {
            r.set(n);
          }

          @Override
          public void cancel() {}
        });
    assertEquals(99, r.get());
    assertEquals(10, innerR.get());
  }
コード例 #15
0
  /**
   * @param depMode Deployment mode.
   * @param ldr Class loader to deploy.
   * @param cls Class.
   * @param alias Class alias.
   * @return Deployment.
   */
  @SuppressWarnings({"ConstantConditions"})
  private GridDeployment deploy(
      GridDeploymentMode depMode, ClassLoader ldr, Class<?> cls, String alias) {
    assert Thread.holdsLock(mux);

    LinkedList<GridDeployment> cachedDeps = null;

    GridDeployment dep = null;

    // Find existing class loader info.
    for (LinkedList<GridDeployment> deps : cache.values()) {
      for (GridDeployment d : deps) {
        if (d.classLoader() == ldr) {
          // Cache class and alias.
          d.addDeployedClass(cls, alias);

          cachedDeps = deps;

          dep = d;

          break;
        }
      }

      if (cachedDeps != null) {
        break;
      }
    }

    if (cachedDeps != null) {
      assert dep != null;

      cache.put(alias, cachedDeps);

      if (!cls.getName().equals(alias)) {
        // Cache by class name as well.
        cache.put(cls.getName(), cachedDeps);
      }

      return dep;
    }

    GridUuid ldrId = GridUuid.randomUuid();

    long seqNum = seq.incrementAndGet();

    String userVer = getUserVersion(ldr);

    dep = new GridDeployment(depMode, ldr, ldrId, seqNum, userVer, cls.getName(), true);

    dep.addDeployedClass(cls, alias);

    LinkedList<GridDeployment> deps =
        F.addIfAbsent(cache, alias, F.<GridDeployment>newLinkedList());

    if (!deps.isEmpty()) {
      for (GridDeployment d : deps) {
        if (!d.isUndeployed()) {
          U.error(
              log,
              "Found more than one active deployment for the same resource "
                  + "[cls="
                  + cls
                  + ", depMode="
                  + depMode
                  + ", dep="
                  + d
                  + ']');

          return null;
        }
      }
    }

    // Add at the beginning of the list for future fast access.
    deps.addFirst(dep);

    if (!cls.getName().equals(alias)) {
      // Cache by class name as well.
      cache.put(cls.getName(), deps);
    }

    if (log.isDebugEnabled()) {
      log.debug("Created new deployment: " + dep);
    }

    return dep;
  }
コード例 #16
0
    /**
     * @param time Test time.
     * @param writers Number of writers threads.
     * @throws Exception If failed.
     */
    public void testQueue(long time, int writers) throws Exception {
      System.out.println("Start test [writers=" + writers + ", time=" + time + "]");

      Thread rdr =
          new Thread() {
            @SuppressWarnings({"InfiniteLoopStatement", "unchecked"})
            @Override
            public void run() {
              try {
                while (true) {
                  Future fut;

                  while ((fut = queue.poll()) != null) {
                    qSize.decrementAndGet();

                    fut.onDone(true);
                  }

                  long size = qSize.get();

                  if (size == 0) {
                    synchronized (mux) {
                      while (queue.isEmpty()) {
                        mux.wait();
                      }
                    }
                  }
                }
              } catch (InterruptedException ignore) {
              }
            }
          };

      rdr.start();

      List<Thread> putThreads = new ArrayList<>();

      for (int i = 0; i < writers; i++) {
        putThreads.add(
            new Thread() {
              @SuppressWarnings("CallToNotifyInsteadOfNotifyAll")
              @Override
              public void run() {
                try {
                  while (!stop) {
                    long id = cnt.incrementAndGet();

                    Future<Boolean> fut = new Future<Boolean>(new Message(id));

                    queue.offer(fut);

                    long size = qSize.incrementAndGet();

                    if (size == 1) {
                      synchronized (mux) {
                        mux.notify();
                      }
                    }

                    Boolean res = fut.get();

                    if (!res) {
                      System.out.println("Error");
                    }
                  }
                } catch (Exception e) {
                  e.printStackTrace();
                }
              }
            });
      }

      for (Thread t : putThreads) t.start();

      Thread.sleep(time);

      stop = true;

      for (Thread t : putThreads) t.join();

      rdr.interrupt();

      rdr.join();

      System.out.println("Total: " + cnt.get());

      System.gc();
      System.gc();
      System.gc();
    }