/** {@inheritDoc} */
  @Override
  public final void removeX() throws GridException {
    if (closed.get()) throw new NoSuchElementException("Iterator has been closed.");

    try {
      onRemove();
    } catch (GridException e) {
      if (!closed.get()) throw e;
    }
  }
  /** {@inheritDoc} */
  @Override
  public final boolean hasNextX() throws GridException {
    if (closed.get()) return false;

    try {
      return onHasNext();
    } catch (GridException e) {
      if (closed.get()) return false;
      else throw e;
    }
  }
  /** {@inheritDoc} */
  @Override
  public final T nextX() throws GridException {
    if (closed.get()) return null;

    try {
      if (!onHasNext()) throw new NoSuchElementException();

      return onNext();
    } catch (GridException e) {
      if (closed.get()) return null;
      else throw e;
    }
  }
  /** {@inheritDoc} */
  @Override
  public final void onKernalStop(boolean cancel) {
    if (!starting.get())
      // Ignoring attempt to stop manager that has never been started.
      return;

    onKernalStop0(cancel);

    if (log != null && log.isDebugEnabled()) log.debug(kernalStopInfo());
  }
  /** {@inheritDoc} */
  @Override
  public final void stop(boolean cancel) {
    if (!starting.get() || !stop.compareAndSet(false, true))
      // Ignoring attempt to stop manager that has never been started.
      return;

    stop0(cancel);

    if (log != null && log.isDebugEnabled()) log.debug(stopInfo());
  }
Beispiel #6
0
  public void testRPCInterrupted() throws IOException, InterruptedException {
    final MiniDFSCluster cluster;
    Configuration conf = new Configuration();
    cluster = new MiniDFSCluster(conf, 3, true, null);
    final AtomicBoolean passed = new AtomicBoolean(false);
    try {
      cluster.waitActive();
      Thread rpcThread =
          new Thread(
              new Runnable() {
                @Override
                public void run() {
                  FileSystem fs = null;
                  try {
                    fs = cluster.getUniqueFileSystem();
                    int i = 0;
                    while (true) {
                      fs.create(new Path(String.format("/file-%09d", i++)));
                      fs.listStatus(new Path("/"));
                    }
                  } catch (IOException e) {
                    if (e.getCause() instanceof InterruptedException) {
                      // the underlying InterruptedException should be wrapped to an
                      // IOException and end up here
                      passed.set(true);
                    } else {
                      passed.set(false);
                      fail(e.getMessage());
                    }
                  } finally {
                    if (fs != null) {
                      try {
                        fs.close();
                      } catch (IOException e) {
                        passed.set(false);
                        LOG.error(e);
                        fail(e.toString());
                      }
                    }
                  }
                }
              });
      FileSystem fs2 = cluster.getUniqueFileSystem();

      rpcThread.start();
      Thread.sleep(1000);
      rpcThread.interrupt();
      rpcThread.join();
      FileStatus[] statuses = fs2.listStatus(new Path("/"));
      assertTrue("expect at least 1 file created", statuses.length > 0);
      assertTrue("error in writing thread, see above", passed.get());
    } finally {
      cluster.shutdown();
    }
  }
Beispiel #7
0
  public static void main(String[] args) throws Exception {
    final Config config = Config.loadFromDisk("nexus-importer");
    final HTTPCache http = HttpClient.createHttpCache(config);
    final XmlParser xmlParser = new XmlParser();
    final BoneCPDataSource boneCp = config.createBoneCp();

    XmlParser.debugXml = false;

    ObjectManager<NexusServerDto, ActorRef<NexusServer>> serverManager =
        new ObjectManager<>(
            "Nexus server",
            Collections.<NexusServerDto>emptySet(),
            new ObjectFactory<NexusServerDto, ActorRef<NexusServer>>() {
              public ActorRef<NexusServer> create(NexusServerDto server) {
                final NexusClient client = new NexusClient(http, server.url);

                String name = server.name;

                return ObjectUtil.threadedActor(
                    name,
                    config.nexusUpdateInterval,
                    boneCp,
                    "Nexus Server: " + name,
                    new NexusServer(client, server, xmlParser));
              }
            });

    final AtomicBoolean shouldRun = new AtomicBoolean(true);
    config.addShutdownHook(currentThread(), shouldRun);

    while (shouldRun.get()) {
      try {
        List<NexusServerDto> newKeys;

        try (Connection c = boneCp.getConnection()) {
          newKeys = new NexusDao(c).selectServer();
        }

        serverManager.update(newKeys);
      } catch (SQLException e) {
        e.printStackTrace(System.out);
      }

      synchronized (shouldRun) {
        shouldRun.wait(60 * 1000);
      }
    }

    serverManager.close();
  }
  /**
   * Asynchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Future indicating sequence value.
   * @throws GridException If update failed.
   */
  private GridFuture<Long> internalUpdateAsync(
      long l, @Nullable Callable<Long> updateCall, boolean updated) throws GridException {
    checkRemoved();

    A.ensure(l > 0, " Parameter mustn't be less then 1: " + l);

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return ctx.closures().callLocalSafe(updateCall, true);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return new GridFinishedFuture<Long>(ctx.kernalContext(), updated ? locVal : curVal);
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }
  /**
   * Synchronous sequence update operation. Will add given amount to the sequence value.
   *
   * @param l Increment amount.
   * @param updateCall Cache call that will update sequence reservation count in accordance with l.
   * @param updated If {@code true}, will return sequence value after update, otherwise will return
   *     sequence value prior to update.
   * @return Sequence value.
   * @throws GridException If update failed.
   */
  private long internalUpdate(long l, @Nullable Callable<Long> updateCall, boolean updated)
      throws GridException {
    checkRemoved();

    assert l > 0;

    lock.lock();

    try {
      // If reserved range isn't exhausted.
      if (locVal + l <= upBound) {
        long curVal = locVal;

        locVal += l;

        return updated ? locVal : curVal;
      }
    } finally {
      lock.unlock();
    }

    if (updateCall == null) updateCall = internalUpdate(l, updated);

    while (true) {
      if (updateGuard.compareAndSet(false, true)) {
        try {
          // This call must be outside lock.
          return CU.outTx(updateCall, ctx);
        } finally {
          lock.lock();

          try {
            updateGuard.set(false);

            cond.signalAll();
          } finally {
            lock.unlock();
          }
        }
      } else {
        lock.lock();

        try {
          while (locVal >= upBound && updateGuard.get()) {
            try {
              cond.await(500, MILLISECONDS);
            } catch (InterruptedException e) {
              throw new GridInterruptedException(e);
            }
          }

          checkRemoved();

          // If reserved range isn't exhausted.
          if (locVal + l <= upBound) {
            long curVal = locVal;

            locVal += l;

            return updated ? locVal : curVal;
          }
        } finally {
          lock.unlock();
        }
      }
    }
  }
Beispiel #10
0
  @Override
  protected <T, E> Predicate<T> createPredicate(final EntityCache<T> cache) {
    if (column == null && this.nodes == null) return null;
    final EntityCache<E> joinCache = this.joinEntity.getCache();
    final AtomicBoolean more = new AtomicBoolean();
    Predicate<E> filter = createJoinPredicate(more);
    Predicate<T> rs = null;
    if (filter == null && !more.get()) return rs;
    if (filter != null) {
      final Predicate<E> inner = filter;
      rs =
          new Predicate<T>() {

            @Override
            public boolean test(final T t) {
              Predicate<E> joinPredicate = null;
              for (String joinColumn : joinColumns) {
                final Serializable key = cache.getAttribute(joinColumn).get(t);
                final Attribute<E, Serializable> joinAttr = joinCache.getAttribute(joinColumn);
                Predicate<E> p = (E e) -> key.equals(joinAttr.get(e));
                joinPredicate = joinPredicate == null ? p : joinPredicate.and(p);
              }
              return joinCache.exists(inner.and(joinPredicate));
            }

            @Override
            public String toString() {
              StringBuilder sb = new StringBuilder();
              sb.append(" #-- ON ")
                  .append(joinColumns[0])
                  .append("=")
                  .append(joinClass == null ? "null" : joinClass.getSimpleName())
                  .append(".")
                  .append(joinColumns[0]);
              for (int i = 1; i < joinColumns.length; i++) {
                sb.append(" AND ")
                    .append(joinColumns[i])
                    .append("=")
                    .append(joinClass == null ? "null" : joinClass.getSimpleName())
                    .append(".")
                    .append(joinColumns[i]);
              }
              sb.append(" --# ").append(inner.toString());
              return sb.toString();
            }
          };
    }
    if (more.get()) { // 存在不同Class的关联表
      if (this.nodes != null) {
        for (FilterNode node : this.nodes) {
          if (((FilterJoinNode) node).joinClass == this.joinClass) continue;
          Predicate<T> f = node.createPredicate(cache);
          if (f == null) continue;
          final Predicate<T> one = rs;
          final Predicate<T> two = f;
          rs =
              (rs == null)
                  ? f
                  : (or
                      ? new Predicate<T>() {

                        @Override
                        public boolean test(T t) {
                          return one.test(t) || two.test(t);
                        }

                        @Override
                        public String toString() {
                          return "(" + one + " OR " + two + ")";
                        }
                      }
                      : new Predicate<T>() {

                        @Override
                        public boolean test(T t) {
                          return one.test(t) && two.test(t);
                        }

                        @Override
                        public String toString() {
                          return "(" + one + " AND " + two + ")";
                        }
                      });
        }
      }
    }
    return rs;
  }
 @Override
 public boolean isWaitingForPairing() {
   return waitingForPairing.get();
 }
  private void runTestUnfinishedBlockCRCError(
      final boolean transferToAllowed,
      final SyncType syncType,
      final int writeSize,
      Configuration conf)
      throws IOException {
    conf.setBoolean("dfs.support.append", syncType == SyncType.APPEND);
    conf.setBoolean("dfs.datanode.transferTo.allowed", transferToAllowed);
    init(conf);

    final Path file = new Path("/block-being-written-to");
    final int numWrites = 2000;
    final AtomicBoolean writerDone = new AtomicBoolean(false);
    final AtomicBoolean writerStarted = new AtomicBoolean(false);
    final AtomicBoolean error = new AtomicBoolean(false);
    final FSDataOutputStream initialOutputStream = fileSystem.create(file);
    final Thread writer =
        new Thread(
            new Runnable() {
              private FSDataOutputStream outputStream = initialOutputStream;

              @Override
              public void run() {
                try {
                  for (int i = 0; !error.get() && i < numWrites; i++) {
                    try {
                      final byte[] writeBuf =
                          DFSTestUtil.generateSequentialBytes(i * writeSize, writeSize);
                      outputStream.write(writeBuf);
                      if (syncType == SyncType.SYNC) {
                        outputStream.hflush();
                      } else { // append
                        outputStream.close();
                        outputStream = fileSystem.append(file);
                      }
                      writerStarted.set(true);
                    } catch (IOException e) {
                      error.set(true);
                      LOG.error("error writing to file", e);
                    }
                  }

                  writerDone.set(true);
                  outputStream.close();
                } catch (Exception e) {
                  LOG.error("error in writer", e);

                  throw new RuntimeException(e);
                }
              }
            });
    Thread tailer =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                try {
                  long startPos = 0;
                  while (!writerDone.get() && !error.get()) {
                    if (writerStarted.get()) {
                      try {
                        startPos = tailFile(file, startPos);
                      } catch (IOException e) {
                        LOG.error(String.format("error tailing file %s", file), e);

                        throw new RuntimeException(e);
                      }
                    }
                  }
                } catch (RuntimeException e) {
                  if (e.getCause() instanceof ChecksumException) {
                    error.set(true);
                  }

                  writer.interrupt();
                  LOG.error("error in tailer", e);
                  throw e;
                }
              }
            });

    writer.start();
    tailer.start();

    try {
      writer.join();
      tailer.join();

      assertFalse("error occurred, see log above", error.get());
    } catch (InterruptedException e) {
      LOG.info("interrupted waiting for writer or tailer to complete");

      Thread.currentThread().interrupt();
    }
    initialOutputStream.close();
  }
  /**
   * Grabs local events and detects if events was lost since last poll.
   *
   * @param ignite Target grid.
   * @param evtOrderKey Unique key to take last order key from node local map.
   * @param evtThrottleCntrKey Unique key to take throttle count from node local map.
   * @param evtTypes Event types to collect.
   * @param evtMapper Closure to map grid events to Visor data transfer objects.
   * @return Collections of node events
   */
  public static Collection<VisorGridEvent> collectEvents(
      Ignite ignite,
      String evtOrderKey,
      String evtThrottleCntrKey,
      final int[] evtTypes,
      IgniteClosure<Event, VisorGridEvent> evtMapper) {
    assert ignite != null;
    assert evtTypes != null && evtTypes.length > 0;

    ConcurrentMap<String, Long> nl = ignite.cluster().nodeLocalMap();

    final long lastOrder = getOrElse(nl, evtOrderKey, -1L);
    final long throttle = getOrElse(nl, evtThrottleCntrKey, 0L);

    // When we first time arrive onto a node to get its local events,
    // we'll grab only last those events that not older than given period to make sure we are
    // not grabbing GBs of data accidentally.
    final long notOlderThan = System.currentTimeMillis() - EVENTS_COLLECT_TIME_WINDOW;

    // Flag for detecting gaps between events.
    final AtomicBoolean lastFound = new AtomicBoolean(lastOrder < 0);

    IgnitePredicate<Event> p =
        new IgnitePredicate<Event>() {
          /** */
          private static final long serialVersionUID = 0L;

          @Override
          public boolean apply(Event e) {
            // Detects that events were lost.
            if (!lastFound.get() && (lastOrder == e.localOrder())) lastFound.set(true);

            // Retains events by lastOrder, period and type.
            return e.localOrder() > lastOrder
                && e.timestamp() > notOlderThan
                && F.contains(evtTypes, e.type());
          }
        };

    Collection<Event> evts = ignite.events().localQuery(p);

    // Update latest order in node local, if not empty.
    if (!evts.isEmpty()) {
      Event maxEvt = Collections.max(evts, EVTS_ORDER_COMPARATOR);

      nl.put(evtOrderKey, maxEvt.localOrder());
    }

    // Update throttle counter.
    if (!lastFound.get())
      nl.put(evtThrottleCntrKey, throttle == 0 ? EVENTS_LOST_THROTTLE : throttle - 1);

    boolean lost = !lastFound.get() && throttle == 0;

    Collection<VisorGridEvent> res = new ArrayList<>(evts.size() + (lost ? 1 : 0));

    if (lost) res.add(new VisorGridEventsLost(ignite.cluster().localNode().id()));

    for (Event e : evts) {
      VisorGridEvent visorEvt = evtMapper.apply(e);

      if (visorEvt != null) res.add(visorEvt);
    }

    return res;
  }
 /** {@inheritDoc} */
 @Override
 public boolean isClosed() {
   return closed.get();
 }