Beispiel #1
0
  ImmutableSet<ListenableFuture<?>> index(List<Span> spans) {
    // First parse each span into partition keys used to support query requests
    Builder<PartitionKeyToTraceId, Long> parsed = ImmutableSetMultimap.builder();
    for (Span span : spans) {
      Long timestamp = guessTimestamp(span);
      if (timestamp == null) continue;
      for (String partitionKey : index.partitionKeys(span)) {
        parsed.put(
            new PartitionKeyToTraceId(index.table(), partitionKey, span.traceId),
            1000 * (timestamp / 1000)); // index precision is millis
      }
    }

    // The parsed results may include inserts that already occur, or are redundant as they don't
    // impact QueryRequest.endTs or QueryRequest.loopback. For example, a parsed timestamp could
    // be between timestamps of rows that already exist for a particular trace.
    ImmutableSetMultimap<PartitionKeyToTraceId, Long> maybeInsert = parsed.build();

    ImmutableSetMultimap<PartitionKeyToTraceId, Long> toInsert;
    if (sharedState == null) { // special-case when caching is disabled.
      toInsert = maybeInsert;
    } else {
      // Optimized results will be smaller when the input includes traces with local spans, or when
      // other threads indexed the same trace.
      toInsert = entriesThatIncreaseGap(sharedState, maybeInsert);

      if (maybeInsert.size() > toInsert.size() && LOG.isDebugEnabled()) {
        int delta = maybeInsert.size() - toInsert.size();
        LOG.debug("optimized out {}/{} inserts into {}", delta, maybeInsert.size(), index.table());
      }
    }

    // For each entry, insert a new row in the index table asynchronously
    ImmutableSet.Builder<ListenableFuture<?>> result = ImmutableSet.builder();
    for (Map.Entry<PartitionKeyToTraceId, Long> entry : toInsert.entries()) {
      BoundStatement bound =
          bindWithName(prepared, boundName)
              .setLong("trace_id", entry.getKey().traceId)
              .setBytesUnsafe("ts", timestampCodec.serialize(entry.getValue()));
      if (indexTtl != null) {
        bound.setInt("ttl_", indexTtl);
      }
      index.bindPartitionKey(bound, entry.getKey().partitionKey);
      result.add(session.executeAsync(bound));
    }
    return result.build();
  }
  @Test
  public void testMultiFSPolicy() throws Exception {
    File globalPolicyFile = new File(Files.createTempDir(), "global-policy.ini");
    File dbPolicyFile = new File(Files.createTempDir(), "db11-policy.ini");

    // Create global policy file
    PolicyFile dbPolicy =
        new PolicyFile()
            .addPermissionsToRole("db11_role", "server=server1->db=db11")
            .addRolesToGroup("group1", "db11_role");

    dbPolicy.write(dbPolicyFile);
    Path dbPolicyPath = new Path(etc, "db11-policy.ini");

    // create per-db policy file
    PolicyFile globalPolicy =
        new PolicyFile()
            .addPermissionsToRole("admin_role", "server=server1")
            .addRolesToGroup("admin_group", "admin_role")
            .addGroupsToUser("db", "admin_group");
    globalPolicy.addDatabase("db11", dbPolicyPath.toUri().toString());
    globalPolicy.write(globalPolicyFile);

    PolicyFiles.copyFilesToDir(fileSystem, etc, globalPolicyFile);
    PolicyFiles.copyFilesToDir(fileSystem, etc, dbPolicyFile);
    DBPolicyFileBackend multiFSEngine =
        new DBPolicyFileBackend(globalPolicyFile.getPath(), "server1");

    List<Authorizable> dbAuthorizables = Lists.newArrayList();
    dbAuthorizables.add(new Server("server1"));
    dbAuthorizables.add(new Database("db11"));
    List<String> dbGroups = Lists.newArrayList();
    dbGroups.add("group1");
    ImmutableSetMultimap<String, String> dbPerms =
        multiFSEngine.getPermissions(dbAuthorizables, dbGroups);
    Assert.assertEquals("No DB permissions found", 1, dbPerms.size());
  }
  public static void findPlugins(boolean scanClasspath) throws IOException {
    SetMultimap<Object, String> found = LinkedHashMultimap.create();
    Set<String> pluginClasses = null;

    VanillaLaunch.getLogger().info("Searching for plugins...");

    if (scanClasspath) {
      VanillaLaunch.getLogger().info("Scanning classpath for plugins...");

      ClassLoader loader = VanillaLaunch.class.getClassLoader();
      if (loader instanceof URLClassLoader) {
        pluginClasses = PluginScanner.scanClassPath((URLClassLoader) loader);
        found.putAll("classpath", pluginClasses);
      } else {
        VanillaLaunch.getLogger()
            .error(
                "Cannot search for plugins on classpath: Unsupported class loader: {}",
                loader.getClass());
      }
    }

    Path pluginsDir = SpongeLaunch.getPluginsDir();
    if (Files.exists(pluginsDir)) {
      if (pluginClasses == null) {
        pluginClasses = new HashSet<>();
      }

      List<Path> paths;
      try (DirectoryStream<Path> dir =
          Files.newDirectoryStream(SpongeLaunch.getPluginsDir(), ARCHIVE_FILTER)) {
        paths = Lists.newArrayList(dir);
      }

      Collections.sort(paths);

      for (Path path : paths) {
        // Search for plugins in the JAR
        try (JarFile jar = new JarFile(path.toFile())) {
          Set<String> plugins = PluginScanner.scanZip(path, jar);

          Iterator<String> itr = plugins.iterator();
          while (itr.hasNext()) {
            String plugin = itr.next();
            if (!pluginClasses.add(plugin)) {
              VanillaLaunch.getLogger()
                  .warn("Skipping duplicate plugin class {} from {}", plugin, path);
              itr.remove();
            }
          }

          if (!plugins.isEmpty()) {
            found.putAll(path, plugins);

            // Look for access transformers
            PluginAccessTransformers.register(path, jar);
          }
        } catch (IOException e) {
          VanillaLaunch.getLogger().error("Failed to scan plugin JAR: {}", path, e);
        }
      }
    } else {
      // Create plugin folder
      Files.createDirectories(pluginsDir);
    }

    plugins = ImmutableSetMultimap.copyOf(found);
    VanillaLaunch.getLogger().info("{} plugin(s) found", plugins.size());
  }
 @Override
 public int size() {
   return multimap.size();
 }