Esempio n. 1
1
  private Throwable createTestException() {
    Exception exception =
        new Exception() {
          @Override
          public String toString() {
            return "custom msg";
          }
        };
    exception.setStackTrace(
        new StackTraceElement[] {
          new StackTraceElement("c1", "m1", "f1", 1), new StackTraceElement("c2", "m2", "f2", 2)
        });

    Exception cause =
        new Exception() {
          @Override
          public String toString() {
            return "custom msg cause";
          }
        };
    cause.setStackTrace(
        new StackTraceElement[] {
          new StackTraceElement("c3", "m3", "f3", 3), new StackTraceElement("c4", "m4", "f4", 4)
        });
    exception.initCause(cause);

    Exception supressed1 =
        new Exception() {
          @Override
          public String toString() {
            return "custom msg supressed 1";
          }
        };
    supressed1.setStackTrace(new StackTraceElement[] {new StackTraceElement("c5", "m5", "f5", 5)});

    Exception s1Cause =
        new Exception() {
          @Override
          public String toString() {
            return "custom msg supressed 1 cause";
          }
        };
    s1Cause.setStackTrace(new StackTraceElement[] {new StackTraceElement("c6", "m6", "f6", 6)});
    supressed1.initCause(s1Cause);

    exception.addSuppressed(supressed1);

    Exception s2 =
        new Exception() {
          @Override
          public String toString() {
            return "custom msg supressed 2";
          }
        };
    s2.setStackTrace(new StackTraceElement[] {new StackTraceElement("c7", "m7", "f7", 7)});
    exception.addSuppressed(s2);

    return exception;
  }
 @Override
 public void onAllNodesAcked(@Nullable Exception e) {
   try {
     listener.onAllNodesAcked(e);
   } catch (Exception inner) {
     inner.addSuppressed(e);
     logger.error("exception thrown by listener while notifying on all nodes acked", inner);
   }
 }
Esempio n. 3
1
    /**
     * Add an exception to the result. All exceptions after the first become suppressed exceptions
     * hanging off the first.
     *
     * @param exception the exception to add
     */
    private void addException(final Exception exception) {
      Preconditions.checkNotNull(exception);

      if (resultException == null) {
        resultException = exception;
      } else {
        resultException.addSuppressed(exception);
      }
    }
  @Override
  public void copyFrom(Directory from, String srcFile, String destFile, IOContext context)
      throws IOException {
    final Directory fromUnwrapped = FilterDirectory.unwrap(from);
    final Directory toUnwrapped = FilterDirectory.unwrap(this);
    // try to unwrap to FSDirectory - we might be able to just create hard-links of these files and
    // save copying
    // the entire file.
    Exception suppressedException = null;
    boolean tryCopy = true;
    if (fromUnwrapped instanceof FSDirectory && toUnwrapped instanceof FSDirectory) {
      final Path fromPath = ((FSDirectory) fromUnwrapped).getDirectory();
      final Path toPath = ((FSDirectory) toUnwrapped).getDirectory();

      if (Files.isReadable(fromPath.resolve(srcFile)) && Files.isWritable(toPath)) {
        // only try hardlinks if we have permission to access the files
        // if not super.copyFrom() will give us the right exceptions
        suppressedException =
            AccessController.doPrivileged(
                (PrivilegedAction<Exception>)
                    () -> {
                      try {
                        Files.createLink(toPath.resolve(destFile), fromPath.resolve(srcFile));
                      } catch (FileNotFoundException
                          | NoSuchFileException
                          | FileAlreadyExistsException ex) {
                        return ex; // in these cases we bubble up since it's a true error condition.
                      } catch (IOException
                          | UnsupportedOperationException // if the FS doesn't support hard-links
                          | SecurityException
                              ex // we don't have permission to use hard-links just fall back to
                                 // byte copy
                      ) {
                        // hard-links are not supported or the files are on different filesystems
                        // we could go deeper and check if their filesstores are the same and opt
                        // out earlier but for now we just fall back to normal file-copy
                        return ex;
                      }
                      return null;
                    });
        tryCopy = suppressedException != null;
      }
    }
    if (tryCopy) {
      try {
        super.copyFrom(from, srcFile, destFile, context);
      } catch (Exception ex) {
        if (suppressedException != null) {
          ex.addSuppressed(suppressedException);
        }
        throw ex;
      }
    }
  }
 private void raiseEarlyFailure(Exception e) {
   for (AtomicArray.Entry<FirstResult> entry : firstResults.asList()) {
     try {
       DiscoveryNode node = nodeIdToDiscoveryNode.apply(entry.value.shardTarget().nodeId());
       sendReleaseSearchContext(entry.value.id(), node);
     } catch (Exception inner) {
       inner.addSuppressed(e);
       logger.trace("failed to release context", inner);
     }
   }
   listener.onFailure(e);
 }
 @Override
 public void onFailure(String source, Exception e) {
   try {
     listener.onFailure(source, e);
   } catch (Exception inner) {
     inner.addSuppressed(e);
     logger.error(
         (Supplier<?>)
             () ->
                 new ParameterizedMessage(
                     "exception thrown by listener notifying of failure from [{}]", source),
         inner);
   }
 }
 @Override
 public void changed(ObservableValue<? extends T> sourceProperty, T oldValue, T newValue) {
   if (!updating) {
     final Property<T> property1 = propertyRef1.get();
     final Property<T> property2 = propertyRef2.get();
     if ((property1 == null) || (property2 == null)) {
       if (property1 != null) {
         property1.removeListener(this);
       }
       if (property2 != null) {
         property2.removeListener(this);
       }
     } else {
       try {
         updating = true;
         if (property1 == sourceProperty) {
           property2.setValue(newValue);
         } else {
           property1.setValue(newValue);
         }
       } catch (RuntimeException e) {
         try {
           if (property1 == sourceProperty) {
             property1.setValue(oldValue);
           } else {
             property2.setValue(oldValue);
           }
         } catch (Exception e2) {
           e2.addSuppressed(e);
           unbind(property1, property2);
           throw new RuntimeException(
               "Bidirectional binding failed together with an attempt"
                   + " to restore the source property to the previous value."
                   + " Removing the bidirectional binding from properties "
                   + property1
                   + " and "
                   + property2,
               e2);
         }
         throw new RuntimeException(
             "Bidirectional binding failed, setting to the previous value", e);
       } finally {
         updating = false;
       }
     }
   }
 }
 private Exception checkForExceptions(String message, Collection<Future<JettySolrRunner>> futures)
     throws InterruptedException {
   Exception parsed = new Exception(message);
   boolean ok = true;
   for (Future<JettySolrRunner> future : futures) {
     try {
       future.get();
     } catch (ExecutionException e) {
       parsed.addSuppressed(e.getCause());
       ok = false;
     } catch (InterruptedException e) {
       Thread.interrupted();
       throw e;
     }
   }
   return ok ? null : parsed;
 }
 @Override
 public synchronized void stop(BundleContext context) throws Exception {
   Exception lastException = null;
   for (AutoCloseable autoCloseable : autoCloseables) {
     try {
       autoCloseable.close();
     } catch (Exception e) {
       if (lastException == null) {
         lastException = e;
       } else {
         lastException.addSuppressed(e);
       }
     }
   }
   if (lastException != null) {
     throw lastException;
   }
 }
Esempio n. 10
0
    private void installFile(
        IDevice device, final int port, Path pathRelativeToDataRoot, final Path relativeSource)
        throws Exception {
      final Path source = projectFilesystem.resolve(relativeSource);
      CollectingOutputReceiver receiver =
          new CollectingOutputReceiver() {

            private boolean sentPayload = false;

            @Override
            public void addOutput(byte[] data, int offset, int length) {
              super.addOutput(data, offset, length);
              if (!sentPayload && getOutput().length() >= AgentUtil.TEXT_SECRET_KEY_SIZE) {
                LOG.verbose("Got key: %s", getOutput().trim());

                sentPayload = true;
                try (Socket clientSocket = new Socket("localhost", port)) {
                  LOG.verbose("Connected");
                  OutputStream outToDevice = clientSocket.getOutputStream();
                  outToDevice.write(
                      getOutput().substring(0, AgentUtil.TEXT_SECRET_KEY_SIZE).getBytes());
                  LOG.verbose("Wrote key");
                  com.google.common.io.Files.asByteSource(source.toFile()).copyTo(outToDevice);
                  LOG.verbose("Wrote file");
                } catch (IOException e) {
                  throw new RuntimeException(e);
                }
              }
            }
          };

      String targetFileName =
          projectFilesystem.resolve(dataRoot.resolve(pathRelativeToDataRoot)).toString();
      String command =
          "umask 022 && "
              + getAgentCommand()
              + "receive-file "
              + port
              + " "
              + Files.size(source)
              + " "
              + targetFileName
              + " ; echo -n :$?";
      LOG.debug("Executing %s", command);

      // If we fail to execute the command, stash the exception.  My experience during development
      // has been that the exception from checkReceiverOutput is more actionable.
      Exception shellException = null;
      try {
        device.executeShellCommand(command, receiver);
      } catch (Exception e) {
        shellException = e;
      }

      try {
        AdbHelper.checkReceiverOutput(command, receiver);
      } catch (Exception e) {
        if (shellException != null) {
          e.addSuppressed(shellException);
        }
        throw e;
      }

      if (shellException != null) {
        throw shellException;
      }

      // The standard Java libraries on Android always create new files un-readable by other users.
      // We use the shell user or root to create these files, so we need to explicitly set the mode
      // to allow the app to read them.  Ideally, the agent would do this automatically, but
      // there's no easy way to do this in Java.  We can drop this if we drop support for the
      // Java agent.
      AdbHelper.executeCommandWithErrorChecking(device, "chmod 644 " + targetFileName);
    }
Esempio n. 11
0
  @SuppressWarnings("unchecked")
  private boolean registerEntity() {

    try {
      Class<EntityTypes> entityTypeClass = EntityTypes.class;

      Field c = entityTypeClass.getDeclaredField("c");
      c.setAccessible(true);
      HashMap<String, Class<?>> c_map = (HashMap<String, Class<?>>) c.get(null);
      c_map.put("RyeDragon", this.dragonClass);

      Field d = entityTypeClass.getDeclaredField("d");
      d.setAccessible(true);
      HashMap<Class<?>, String> d_map = (HashMap<Class<?>, String>) d.get(null);
      d_map.put(this.dragonClass, "RyeDragon");

      Field e = entityTypeClass.getDeclaredField("e");
      e.setAccessible(true);
      HashMap<Integer, Class<?>> e_map = (HashMap<Integer, Class<?>>) e.get(null);
      e_map.put(Integer.valueOf(63), this.dragonClass);

      Field f = entityTypeClass.getDeclaredField("f");
      f.setAccessible(true);
      HashMap<Class<?>, Integer> f_map = (HashMap<Class<?>, Integer>) f.get(null);
      f_map.put(this.dragonClass, Integer.valueOf(63));

      Field g = entityTypeClass.getDeclaredField("g");
      g.setAccessible(true);
      HashMap<String, Integer> g_map = (HashMap<String, Integer>) g.get(null);
      g_map.put("RyeDragon", Integer.valueOf(63));

      return true;
    } catch (Exception e) {

      Class<?>[] paramTypes = new Class[] {Class.class, String.class, int.class};

      // MCPC+ compatibility
      // Forge Dev environment; names are not translated into func_foo
      try {
        Method method = EntityTypes.class.getDeclaredMethod("addMapping", paramTypes);
        method.setAccessible(true);
        method.invoke(null, RyeDragon.class, "RyeDragon", 63);
        return true;
      } catch (Exception ex) {
        e.addSuppressed(ex);
      }
      // Production environment: search for the method
      // This is required because the seargenames could change
      // LAST CHECKED FOR VERSION 1.6.4
      try {
        for (Method method : EntityTypes.class.getDeclaredMethods()) {
          if (Arrays.equals(paramTypes, method.getParameterTypes())) {
            method.invoke(null, RyeDragon.class, "RyeDragon", 63);
            return true;
          }
        }
      } catch (Exception ex) {
        e.addSuppressed(ex);
      }

      logger.info("[DragonTravel] [Error] Could not register the RyeDragon-entity!");
      e.printStackTrace();
      pm.disablePlugin(this);
    }
    return false;
  }
  private void onFirstPhaseResult(
      final int shardIndex,
      @Nullable ShardRouting shard,
      @Nullable String nodeId,
      final ShardIterator shardIt,
      Exception e) {
    // we always add the shard failure for a specific shard instance
    // we do make sure to clean it on a successful response from a shard
    SearchShardTarget shardTarget = new SearchShardTarget(nodeId, shardIt.shardId());
    addShardFailure(shardIndex, shardTarget, e);

    if (totalOps.incrementAndGet() == expectedTotalOps) {
      if (logger.isDebugEnabled()) {
        if (e != null && !TransportActions.isShardNotAvailableException(e)) {
          logger.debug(
              (Supplier<?>)
                  () ->
                      new ParameterizedMessage(
                          "{}: Failed to execute [{}]",
                          shard != null ? shard.shortSummary() : shardIt.shardId(),
                          request),
              e);
        } else if (logger.isTraceEnabled()) {
          logger.trace(
              (Supplier<?>)
                  () -> new ParameterizedMessage("{}: Failed to execute [{}]", shard, request),
              e);
        }
      }
      final ShardSearchFailure[] shardSearchFailures = buildShardFailures();
      if (successfulOps.get() == 0) {
        if (logger.isDebugEnabled()) {
          logger.debug(
              (Supplier<?>)
                  () ->
                      new ParameterizedMessage(
                          "All shards failed for phase: [{}]", firstPhaseName()),
              e);
        }

        // no successful ops, raise an exception
        raiseEarlyFailure(
            new SearchPhaseExecutionException(
                firstPhaseName(), "all shards failed", e, shardSearchFailures));
      } else {
        try {
          innerMoveToSecondPhase();
        } catch (Exception inner) {
          inner.addSuppressed(e);
          raiseEarlyFailure(
              new ReduceSearchPhaseException(firstPhaseName(), "", inner, shardSearchFailures));
        }
      }
    } else {
      final ShardRouting nextShard = shardIt.nextOrNull();
      final boolean lastShard = nextShard == null;
      // trace log this exception
      logger.trace(
          (Supplier<?>)
              () ->
                  new ParameterizedMessage(
                      "{}: Failed to execute [{}] lastShard [{}]",
                      shard != null ? shard.shortSummary() : shardIt.shardId(),
                      request,
                      lastShard),
          e);
      if (!lastShard) {
        try {
          performFirstPhase(shardIndex, shardIt, nextShard);
        } catch (Exception inner) {
          inner.addSuppressed(e);
          onFirstPhaseResult(shardIndex, shard, shard.currentNodeId(), shardIt, inner);
        }
      } else {
        // no more shards active, add a failure
        if (logger.isDebugEnabled()
            && !logger.isTraceEnabled()) { // do not double log this exception
          if (e != null && !TransportActions.isShardNotAvailableException(e)) {
            logger.debug(
                (Supplier<?>)
                    () ->
                        new ParameterizedMessage(
                            "{}: Failed to execute [{}] lastShard [{}]",
                            shard != null ? shard.shortSummary() : shardIt.shardId(),
                            request,
                            lastShard),
                e);
          }
        }
      }
    }
  }
  /**
   * Create a MiniSolrCloudCluster. Note - this constructor visibility is changed to package
   * protected so as to discourage its usage. Ideally *new* functionality should use {@linkplain
   * SolrCloudTestCase} to configure any additional parameters.
   *
   * @param numServers number of Solr servers to start
   * @param baseDir base directory that the mini cluster should be run from
   * @param solrXml solr.xml file to be uploaded to ZooKeeper
   * @param jettyConfig Jetty configuration
   * @param zkTestServer ZkTestServer to use. If null, one will be created
   * @param securityJson A string representation of security.json file (optional).
   * @throws Exception if there was an error starting the cluster
   */
  MiniSolrCloudCluster(
      int numServers,
      Path baseDir,
      String solrXml,
      JettyConfig jettyConfig,
      ZkTestServer zkTestServer,
      Optional<String> securityJson)
      throws Exception {

    Objects.requireNonNull(securityJson);
    this.baseDir = Objects.requireNonNull(baseDir);
    this.jettyConfig = Objects.requireNonNull(jettyConfig);

    log.info("Starting cluster of {} servers in {}", numServers, baseDir);

    Files.createDirectories(baseDir);

    this.externalZkServer = zkTestServer != null;
    if (!externalZkServer) {
      String zkDir = baseDir.resolve("zookeeper/server1/data").toString();
      zkTestServer = new ZkTestServer(zkDir);
      zkTestServer.run();
    }
    this.zkServer = zkTestServer;

    try (SolrZkClient zkClient =
        new SolrZkClient(zkServer.getZkHost(), AbstractZkTestCase.TIMEOUT)) {
      zkClient.makePath("/solr/solr.xml", solrXml.getBytes(Charset.defaultCharset()), true);
      if (jettyConfig.sslConfig != null && jettyConfig.sslConfig.isSSLMode()) {
        zkClient.makePath(
            "/solr" + ZkStateReader.CLUSTER_PROPS,
            "{'urlScheme':'https'}".getBytes(StandardCharsets.UTF_8),
            true);
      }
      if (securityJson.isPresent()) { // configure Solr security
        zkClient.makePath(
            "/solr/security.json", securityJson.get().getBytes(Charset.defaultCharset()), true);
      }
    }

    // tell solr to look in zookeeper for solr.xml
    System.setProperty("zkHost", zkServer.getZkAddress());

    List<Callable<JettySolrRunner>> startups = new ArrayList<>(numServers);
    for (int i = 0; i < numServers; ++i) {
      startups.add(() -> startJettySolrRunner(newNodeName(), jettyConfig.context, jettyConfig));
    }

    Collection<Future<JettySolrRunner>> futures = executor.invokeAll(startups);
    Exception startupError = checkForExceptions("Error starting up MiniSolrCloudCluster", futures);
    if (startupError != null) {
      try {
        this.shutdown();
      } catch (Throwable t) {
        startupError.addSuppressed(t);
      }
      throw startupError;
    }

    waitForAllNodes(numServers, 60);

    solrClient = buildSolrClient();
  }