Esempio n. 1
0
  /**
   * @param ctx Grid kernal context.
   * @param cacheName Cache name.
   * @param flushQ Flush queue.
   */
  public GridDataLoaderImpl(
      final GridKernalContext ctx,
      @Nullable final String cacheName,
      DelayQueue<GridDataLoaderImpl<K, V>> flushQ) {
    assert ctx != null;

    this.ctx = ctx;
    this.cacheName = cacheName;
    this.flushQ = flushQ;

    log = U.logger(ctx, logRef, GridDataLoaderImpl.class);

    discoLsnr =
        new GridLocalEventListener() {
          @Override
          public void onEvent(GridEvent evt) {
            assert evt.type() == EVT_NODE_FAILED || evt.type() == EVT_NODE_LEFT;

            GridDiscoveryEvent discoEvt = (GridDiscoveryEvent) evt;

            UUID id = discoEvt.eventNodeId();

            // Remap regular mappings.
            final Buffer buf = bufMappings.remove(id);

            if (buf != null) {
              // Only async notification is possible since
              // discovery thread may be trapped otherwise.
              ctx.closure()
                  .callLocalSafe(
                      new Callable<Object>() {
                        @Override
                        public Object call() throws Exception {
                          buf.onNodeLeft();

                          return null;
                        }
                      },
                      true /* system pool */);
            }
          }
        };

    ctx.event().addLocalEventListener(discoLsnr, EVT_NODE_FAILED, EVT_NODE_LEFT);

    // Generate unique topic for this loader.
    topic = TOPIC_DATALOAD.topic(GridUuid.fromUuid(ctx.localNodeId()));

    ctx.io()
        .addMessageListener(
            topic,
            new GridMessageListener() {
              @Override
              public void onMessage(UUID nodeId, Object msg) {
                assert msg instanceof GridDataLoadResponse;

                GridDataLoadResponse res = (GridDataLoadResponse) msg;

                if (log.isDebugEnabled()) log.debug("Received data load response: " + res);

                Buffer buf = bufMappings.get(nodeId);

                if (buf != null) buf.onResponse(res);
                else if (log.isDebugEnabled())
                  log.debug("Ignoring response since node has left [nodeId=" + nodeId + ", ");
              }
            });

    if (log.isDebugEnabled()) log.debug("Added response listener within topic: " + topic);

    fut = new GridDataLoaderFuture(ctx, this);
  }