Exemplo n.º 1
0
 private static int fingerprint(
     final NetworkInfoSource source,
     final List<com.eucalyptus.cluster.Cluster> clusters,
     final Set<String> dirtyPublicAddresses,
     final String networkConfiguration) {
   final HashFunction hashFunction = goodFastHash(32);
   final Hasher hasher = hashFunction.newHasher();
   final Funnel<VersionedNetworkView> versionedItemFunnel =
       new Funnel<VersionedNetworkView>() {
         @Override
         public void funnel(final VersionedNetworkView o, final PrimitiveSink primitiveSink) {
           primitiveSink.putString(o.getId(), StandardCharsets.UTF_8);
           primitiveSink.putChar('=');
           primitiveSink.putInt(o.getVersion());
         }
       };
   for (final Map.Entry<String, Iterable<? extends VersionedNetworkView>> entry :
       source.getView().entrySet()) {
     hasher.putString(entry.getKey(), StandardCharsets.UTF_8);
     for (final VersionedNetworkView item : entry.getValue()) {
       hasher.putObject(item, versionedItemFunnel);
     }
   }
   hasher.putString(
       Joiner.on(',').join(Sets.newTreeSet(Iterables.transform(clusters, HasName.GET_NAME))),
       StandardCharsets.UTF_8);
   hasher.putString(
       Joiner.on(',').join(Sets.newTreeSet(dirtyPublicAddresses)), StandardCharsets.UTF_8);
   hasher.putInt(networkConfiguration.hashCode());
   return hasher.hash().asInt();
 }
Exemplo n.º 2
0
 @Override
 public int hashCode() {
   HashFunction hf = Hashing.murmur3_32();
   Hasher hc = hf.newHasher();
   for (String key : fields.keySet()) {
     hc.putString(key, Charsets.UTF_8);
   }
   return hc.hash().asInt();
 }
 /**
  * 创建注册中心.
  *
  * @param connectString 注册中心连接字符串
  * @param namespace 注册中心命名空间
  * @param digest 注册中心凭证
  * @return 注册中心对象
  */
 public static CoordinatorRegistryCenter createCoordinatorRegistryCenter(
     final String connectString, final String namespace, final Optional<String> digest) {
   Hasher hasher =
       Hashing.md5()
           .newHasher()
           .putString(connectString, Charsets.UTF_8)
           .putString(namespace, Charsets.UTF_8);
   if (digest.isPresent()) {
     hasher.putString(digest.get(), Charsets.UTF_8);
   }
   HashCode hashCode = hasher.hash();
   if (regCenterMap.containsKey(hashCode)) {
     return regCenterMap.get(hashCode);
   }
   ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(connectString, namespace);
   if (digest.isPresent()) {
     zkConfig.setDigest(digest.get());
   }
   CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(zkConfig);
   result.init();
   regCenterMap.putIfAbsent(hashCode, result);
   return result;
 }
  @SuppressWarnings({"rawtypes", "unchecked"})
  private TargetNode<?> createTargetNode(
      BuckEventBus eventBus,
      Cell cell,
      Path buildFile,
      BuildTarget target,
      Map<String, Object> rawNode,
      TargetNodeListener nodeListener) {
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);

    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);

    if (target.isFlavored()) {
      if (description instanceof Flavored) {
        if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) {
          throw new HumanReadableException(
              "Unrecognized flavor in target %s while parsing %s%s.",
              target,
              UnflavoredBuildTarget.BUILD_TARGET_PREFIX,
              MorePaths.pathWithUnixSeparators(
                  target.getBasePath().resolve(cell.getBuildFileName())));
        }
      } else {
        LOG.warn(
            "Target %s (type %s) must implement the Flavored interface "
                + "before we can check if it supports flavors: %s",
            target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
        throw new HumanReadableException(
            "Target %s (type %s) does not currently support flavors (tried %s)",
            target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors());
      }
    }

    Cell targetCell = cell.getCell(target);
    BuildRuleFactoryParams factoryParams =
        new BuildRuleFactoryParams(
            targetCell.getFilesystem(),
            target.withoutCell(),
            new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()),
            targetCell.isEnforcingBuckPackageBoundaries());
    Object constructorArg = description.createUnpopulatedConstructorArg();
    try {
      ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
      ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder();
      try (SimplePerfEvent.Scope scope =
          SimplePerfEvent.scope(
              eventBus, PerfEventId.of("MarshalledConstructorArg"), "target", target)) {
        marshaller.populate(
            targetCell.getCellRoots(),
            targetCell.getFilesystem(),
            factoryParams,
            constructorArg,
            declaredDeps,
            visibilityPatterns,
            rawNode);
      }
      try (SimplePerfEvent.Scope scope =
          SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"), "target", target)) {
        Hasher hasher = Hashing.sha1().newHasher();
        hasher.putString(BuckVersion.getVersion(), UTF_8);
        JsonObjectHashing.hashJsonObject(hasher, rawNode);
        synchronized (this) {
          targetsCornucopia.put(target.getUnflavoredBuildTarget(), target);
        }
        TargetNode<?> node =
            new TargetNode(
                hasher.hash(),
                description,
                constructorArg,
                typeCoercerFactory,
                factoryParams,
                declaredDeps.build(),
                visibilityPatterns.build(),
                targetCell.getCellRoots());
        nodeListener.onCreate(buildFile, node);
        return node;
      }
    } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) {
      throw new HumanReadableException(e);
    } catch (ConstructorArgMarshalException e) {
      throw new HumanReadableException("%s: %s", target, e.getMessage());
    } catch (IOException e) {
      throw new HumanReadableException(e.getMessage(), e);
    }
  }