Java Code Examples for com.google.common.hash.HashCode#fromInt()

The following examples show how to use com.google.common.hash.HashCode#fromInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WorkerFactoryTest.java    From bazel with Apache License 2.0 6 votes vote down vote up
/**
 * Regression test for b/64689608: The execroot of the sandboxed worker process must end with the
 * workspace name, just like the normal execroot does.
 */
@Test
public void sandboxedWorkerPathEndsWithWorkspaceName() throws Exception {
  Path workerBaseDir = fs.getPath("/outputbase/bazel-workers");
  WorkerFactory workerFactory = new WorkerFactory(new WorkerOptions(), workerBaseDir);
  WorkerKey workerKey =
      new WorkerKey(
          /* args= */ ImmutableList.of(),
          /* env= */ ImmutableMap.of(),
          /* execRoot= */ fs.getPath("/outputbase/execroot/workspace"),
          /* mnemonic= */ "dummy",
          /* workerFilesCombinedHash= */ HashCode.fromInt(0),
          /* workerFilesWithHashes= */ ImmutableSortedMap.of(),
          /* mustBeSandboxed= */ true,
          /* proxied= */ false);
  Path sandboxedWorkerPath = workerFactory.getSandboxedWorkerPath(workerKey, 1);

  assertThat(sandboxedWorkerPath.getBaseName()).isEqualTo("workspace");
}
 
Example 2
Source File: ReconstructingStrategy.java    From buck with Apache License 2.0 6 votes vote down vote up
ReconstructingStrategy(
    SourcePathRuleFinder ruleFinder, CellPathResolver cellResolver, Cell rootCell) {
  dataMap = new ConcurrentHashMap<>();
  id = new AtomicInteger();
  delegate =
      (instance, data, children) -> {
        HashCode hash = HashCode.fromInt(id.incrementAndGet());
        dataMap.put(hash, data);
        return hash;
      };
  serializer = new Serializer(ruleFinder, cellResolver, delegate);
  deserializer =
      new Deserializer(
          name ->
              rootCell
                  .getCellProvider()
                  .getCellByPath(cellResolver.getCellPathOrThrow(name))
                  .getFilesystem(),
          Class::forName,
          ruleFinder::getSourcePathResolver,
          rootCell.getToolchainProvider());
}
 
Example 3
Source File: ManifestTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void addEntry() throws IOException {
  Manifest manifest = new Manifest(new RuleKey("cc"));
  RuleKey key = new RuleKey("aa");
  SourcePath input = FakeSourcePath.of("input.h");
  HashCode hashCode = HashCode.fromInt(20);
  FileHashLoader fileHashLoader =
      new FakeFileHashCache(ImmutableMap.of(RESOLVER.getAbsolutePath(input), hashCode));
  manifest.addEntry(
      fileHashLoader, key, RESOLVER, ImmutableSet.of(input), ImmutableSet.of(input));
  assertThat(
      ManifestUtil.toMap(manifest),
      Matchers.equalTo(
          ImmutableMap.of(
              key, ImmutableMap.of(RESOLVER.getRelativePath(input).toString(), hashCode))));
}
 
Example 4
Source File: ManifestTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void lookupMatch() throws IOException {
  RuleKey key = new RuleKey("aa");
  SourcePath input = FakeSourcePath.of("input.h");
  HashCode hashCode = HashCode.fromInt(20);
  Manifest manifest =
      ManifestUtil.fromMap(
          new RuleKey("cc"),
          ImmutableMap.of(
              key, ImmutableMap.of(RESOLVER.getRelativePath(input).toString(), hashCode)));
  FileHashLoader fileHashLoader =
      new FakeFileHashCache(ImmutableMap.of(RESOLVER.getAbsolutePath(input), hashCode));
  assertThat(
      manifest.lookup(fileHashLoader, RESOLVER, ImmutableSet.of(input)),
      Matchers.equalTo(Optional.of(key)));
}
 
Example 5
Source File: Murmur3HashedFrameDecoder.java    From xio with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
  HashCode sentHash = HashCode.fromInt(msg.readInt());
  HashCode hash =
      Hashing.murmur3_32().hashBytes(msg.array(), msg.readerIndex(), msg.readableBytes());

  out.add(msg);

  if (sentHash.equals(hash)) {
    System.out.println("Hashes matches");
  } else {
    System.out.println("Hashes no matches");
  }
}
 
Example 6
Source File: ManifestTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void addEntryFromArchive() throws IOException {
  Manifest manifest = new Manifest(new RuleKey("cc"));
  RuleKey key = new RuleKey("aa");
  ArchiveMemberSourcePath input =
      ArchiveMemberSourcePath.of(FakeSourcePath.of("somewhere/a.jar"), Paths.get("Member.class"));
  HashCode hashCode = HashCode.fromInt(20);
  FileHashLoader fileHashLoader =
      new FakeFileHashCache(
          new HashMap<>(),
          ImmutableMap.of(
              SourcePathFactoryForTests.toAbsoluteArchiveMemberPath(RESOLVER, input), hashCode),
          new HashMap<>());
  manifest.addEntry(
      fileHashLoader, key, RESOLVER, ImmutableSet.of(input), ImmutableSet.of(input));

  assertThat(
      ManifestUtil.toMap(manifest),
      Matchers.equalTo(
          ImmutableMap.of(
              key,
              ImmutableMap.of(
                  ArchiveMemberPath.of(
                          RESOLVER.getRelativePath(input.getArchiveSourcePath()),
                          input.getMemberPath())
                      .toString(),
                  hashCode))));
}
 
Example 7
Source File: WorkerFactoryTest.java    From bazel with Apache License 2.0 4 votes vote down vote up
/** WorkerFactory should create correct worker type based on WorkerKey. */
@Test
public void workerCreationTypeCheck() throws Exception {
  Path workerBaseDir = fs.getPath("/outputbase/bazel-workers");
  WorkerFactory workerFactory = new WorkerFactory(new WorkerOptions(), workerBaseDir);
  WorkerKey sandboxedWorkerKey =
      new WorkerKey(
          /* args= */ ImmutableList.of(),
          /* env= */ ImmutableMap.of(),
          /* execRoot= */ fs.getPath("/outputbase/execroot/workspace"),
          /* mnemonic= */ "dummy",
          /* workerFilesCombinedHash= */ HashCode.fromInt(0),
          /* workerFilesWithHashes= */ ImmutableSortedMap.of(),
          /* mustBeSandboxed= */ true,
          /* proxied= */ false);
  Worker sandboxedWorker = workerFactory.create(sandboxedWorkerKey);
  assertThat(sandboxedWorker.getClass()).isEqualTo(SandboxedWorker.class);

  WorkerKey nonProxiedWorkerKey =
      new WorkerKey(
          /* args= */ ImmutableList.of(),
          /* env= */ ImmutableMap.of(),
          /* execRoot= */ fs.getPath("/outputbase/execroot/workspace"),
          /* mnemonic= */ "dummy",
          /* workerFilesCombinedHash= */ HashCode.fromInt(0),
          /* workerFilesWithHashes= */ ImmutableSortedMap.of(),
          /* mustBeSandboxed= */ false,
          /* proxied= */ false);
  Worker nonProxiedWorker = workerFactory.create(nonProxiedWorkerKey);
  assertThat(nonProxiedWorker.getClass()).isEqualTo(Worker.class);

  WorkerKey proxiedWorkerKey =
      new WorkerKey(
          /* args= */ ImmutableList.of(),
          /* env= */ ImmutableMap.of(),
          /* execRoot= */ fs.getPath("/outputbase/execroot/workspace"),
          /* mnemonic= */ "dummy",
          /* workerFilesCombinedHash= */ HashCode.fromInt(0),
          /* workerFilesWithHashes= */ ImmutableSortedMap.of(),
          /* mustBeSandboxed= */ false,
          /* proxied= */ true);
  Worker proxiedWorker = workerFactory.create(proxiedWorkerKey);
  // If proxied = true, WorkerProxy is created along with a WorkerMultiplexer.
  // Destroy WorkerMultiplexer to avoid unexpected behavior in WorkerMultiplexerManagerTest.
  WorkerMultiplexerManager.removeInstance(proxiedWorkerKey.hashCode());
  assertThat(proxiedWorker.getClass()).isEqualTo(WorkerProxy.class);
}
 
Example 8
Source File: ThriftArtifactCacheTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiContains() throws IOException {
  AtomicReference<BuckCacheResponse> responseRef = new AtomicReference<>();
  HttpService storeClient = new TestHttpService();
  TestHttpService fetchClient =
      new TestHttpService(() -> new InMemoryThriftResponse(responseRef.get()));
  ProjectFilesystem filesystem =
      TestProjectFilesystems.createProjectFilesystem(tempPaths.getRoot());
  ListeningExecutorService service = MoreExecutors.newDirectExecutorService();
  CellPathResolver cellPathResolver = TestCellPathResolver.get(filesystem);
  NetworkCacheArgs networkArgs =
      ImmutableNetworkCacheArgs.builder()
          .setCacheName("default_cache_name")
          .setRepository("default_repository")
          .setCacheReadMode(CacheReadMode.READONLY)
          .setCacheMode(ArtifactCacheMode.thrift_over_http)
          .setScheduleType("default_schedule_type")
          .setTargetConfigurationSerializer(
              TargetConfigurationSerializerForTests.create(cellPathResolver))
          .setUnconfiguredBuildTargetFactory(
              target ->
                  new ParsingUnconfiguredBuildTargetViewFactory()
                      .create(target, cellPathResolver.getCellNameResolver()))
          .setProjectFilesystem(filesystem)
          .setFetchClient(fetchClient)
          .setStoreClient(storeClient)
          .setBuckEventBus(BuckEventBusForTests.newInstance())
          .setHttpWriteExecutorService(service)
          .setHttpFetchExecutorService(service)
          .setErrorTextTemplate("my super error msg")
          .setErrorTextLimit(100)
          .build();

  com.facebook.buck.core.rulekey.RuleKey key0 =
      new com.facebook.buck.core.rulekey.RuleKey(HashCode.fromInt(0));
  com.facebook.buck.core.rulekey.RuleKey key1 =
      new com.facebook.buck.core.rulekey.RuleKey(HashCode.fromInt(1));
  com.facebook.buck.core.rulekey.RuleKey key2 =
      new com.facebook.buck.core.rulekey.RuleKey(HashCode.fromInt(2));
  com.facebook.buck.core.rulekey.RuleKey key3 =
      new com.facebook.buck.core.rulekey.RuleKey(HashCode.fromInt(3));
  ImmutableSet<com.facebook.buck.core.rulekey.RuleKey> ruleKeys =
      ImmutableSet.of(key0, key1, key2, key3);

  BuckCacheMultiContainsResponse multiContainsResponse = new BuckCacheMultiContainsResponse();
  ContainsResult result0 = new ContainsResult().setResultType(DOES_NOT_CONTAIN);
  ContainsResult result1 = new ContainsResult().setResultType(CONTAINS);
  ContainsResult result2 = new ContainsResult().setResultType(UNKNOWN_DUE_TO_TRANSIENT_ERRORS);
  ContainsResult result3 = new ContainsResult().setResultType(CONTAINS);

  multiContainsResponse.addToResults(result0);
  multiContainsResponse.addToResults(result1);
  multiContainsResponse.addToResults(result2);
  multiContainsResponse.addToResults(result3);

  BuckCacheResponse response =
      new BuckCacheResponse()
          .setWasSuccessful(true)
          .setType(BuckCacheRequestType.CONTAINS)
          .setMultiContainsResponse(multiContainsResponse);
  responseRef.set(response);

  try (ThriftArtifactCache cache =
      new ThriftArtifactCache(
          networkArgs,
          "/nice_as_well",
          new BuildId("aabb"),
          1,
          1,
          false,
          "test://",
          "hostname")) {
    AbstractAsynchronousCache.MultiContainsResult result = cache.multiContainsImpl(ruleKeys);
    assertEquals(4, result.getCacheResults().size());
    assertEquals(CacheResultType.MISS, result.getCacheResults().get(key0).getType());
    assertEquals(CacheResultType.CONTAINS, result.getCacheResults().get(key1).getType());
    assertEquals(CacheResultType.ERROR, result.getCacheResults().get(key2).getType());
    assertEquals(CacheResultType.CONTAINS, result.getCacheResults().get(key3).getType());
  }

  assertEquals(1, fetchClient.getCallsCount());
}
 
Example 9
Source File: AbstractAsynchronousCacheTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiFetchLimiting() throws Exception {
  ExplicitRunExecutorService service = new ExplicitRunExecutorService();
  ProjectFilesystem filesystem = new FakeProjectFilesystem();

  List<ImmutableList<RuleKey>> requestedRuleKeys = new ArrayList<>();
  List<ImmutableSet<RuleKey>> checkedRuleKeys = new ArrayList<>();

  try (AbstractAsynchronousCache cache =
      new RequestedKeyRecordingAsynchronousCache(
          service, filesystem, requestedRuleKeys, checkedRuleKeys, 3, 3, false)) {

    List<ListenableFuture<CacheResult>> results = new ArrayList<>();
    List<RuleKey> keys = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      RuleKey key = new RuleKey(HashCode.fromInt(i));
      keys.add(key);
      results.add(
          cache.fetchAsync(null, key, LazyPath.ofInstance(filesystem.getPath("path" + i))));
    }

    service.run();

    for (ListenableFuture<CacheResult> future : results) {
      assertTrue(future.isDone());
      assertTrue(future.get().getType().isSuccess());
    }

    assertEquals(0, checkedRuleKeys.size());

    assertEquals(10, requestedRuleKeys.size());

    // The first couple should be limited by the multiFetchLimit.
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(0), keys.get(1), keys.get(2)), requestedRuleKeys.get(0));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(3), keys.get(4), keys.get(5)), requestedRuleKeys.get(1));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(6), keys.get(7), keys.get(8)), requestedRuleKeys.get(2));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(9), keys.get(1), keys.get(2)), requestedRuleKeys.get(3));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(4), keys.get(5), keys.get(7)), requestedRuleKeys.get(4));

    // At this point, there's just 5 keys left, and so it's limited by the concurrency.
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(8), keys.get(1)), requestedRuleKeys.get(5));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(2), keys.get(5)), requestedRuleKeys.get(6));
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(keys.get(7), keys.get(1)), requestedRuleKeys.get(7));

    // And finally, there's less than concurrency left and it'll go to fetch() instead of
    // multiFetch().
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(5)), requestedRuleKeys.get(8));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(1)), requestedRuleKeys.get(9));
  }
}
 
Example 10
Source File: AbstractAsynchronousCacheTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultiCheck() throws Exception {
  ExplicitRunExecutorService service = new ExplicitRunExecutorService();
  ProjectFilesystem filesystem = new FakeProjectFilesystem();

  List<ImmutableList<RuleKey>> requestedRuleKeys = new ArrayList<>();
  List<ImmutableSet<RuleKey>> checkedRuleKeys = new ArrayList<>();

  try (AbstractAsynchronousCache cache =
      new RequestedKeyRecordingAsynchronousCache(
          service, filesystem, requestedRuleKeys, checkedRuleKeys, 0, 1, true)) {

    List<ListenableFuture<CacheResult>> results = new ArrayList<>();
    List<RuleKey> keys = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
      RuleKey key = new RuleKey(HashCode.fromInt(i));
      keys.add(key);
      results.add(
          cache.fetchAsync(null, key, LazyPath.ofInstance(filesystem.getPath("path" + i))));
    }

    service.run();

    for (int i = 0; i < results.size(); i++) {
      ListenableFuture<CacheResult> future = results.get(i);
      assertTrue(future.isDone());
      if (i < 6) {
        assertTrue(future.get().getType().isSuccess());
      } else {
        assertFalse(future.get().getType().isSuccess());
      }
    }

    assertEquals(1, checkedRuleKeys.size());
    assertEquals(6, requestedRuleKeys.size());

    // Validate we first check if all the keys are present in the cache.
    MoreAsserts.assertIterablesEquals(
        ImmutableList.of(
            keys.get(0),
            keys.get(1),
            keys.get(2),
            keys.get(3),
            keys.get(4),
            keys.get(5),
            keys.get(6),
            keys.get(7),
            keys.get(8),
            keys.get(9)),
        checkedRuleKeys.get(0));

    // Each of the present keys should be download individually.
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(0)), requestedRuleKeys.get(0));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(1)), requestedRuleKeys.get(1));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(2)), requestedRuleKeys.get(2));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(3)), requestedRuleKeys.get(3));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(4)), requestedRuleKeys.get(4));
    MoreAsserts.assertIterablesEquals(ImmutableList.of(keys.get(5)), requestedRuleKeys.get(5));
  }
}
 
Example 11
Source File: ModernBuildRuleRemoteExecutionHelperTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  BuckEventBus eventBus = new DefaultBuckEventBus(FakeClock.doNotCare(), new BuildId("dontcare"));
  ruleFinder =
      new AbstractBuildRuleResolver() {
        @Override
        public Optional<BuildRule> getRuleOptional(BuildTarget buildTarget) {
          return Optional.empty();
        }
      };

  filesystem = new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp.getRoot());
  Cells root = new TestCellBuilder().setFilesystem(filesystem).build();
  mbrHelper =
      new ModernBuildRuleRemoteExecutionHelper(
          eventBus,
          new GrpcProtocol(),
          ruleFinder,
          root.getRootCell(),
          new FileHashCache() {
            @Override
            public HashCode get(Path path) {
              return HashCode.fromInt(0);
            }

            @Override
            public long getSize(Path path) {
              return 0;
            }

            @Override
            public HashCode getForArchiveMember(Path relativeArchivePath, Path memberPath) {
              return HashCode.fromInt(0);
            }

            @Override
            public void invalidate(Path path) {}

            @Override
            public void invalidateAll() {}

            @Override
            public void set(Path path, HashCode hashCode) {}
          },
          ImmutableSet.of());
}
 
Example 12
Source File: ManifestTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void addEntryWithSourcePathsThatHaveSameRelativePaths() throws IOException {
  RuleKey key = new RuleKey("aa");

  Path tmp1 = Files.createTempDirectory("tmp1");
  ProjectFilesystem filesystem1 = new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp1);
  SourcePath input1 = PathSourcePath.of(filesystem1, Paths.get("input.h"));
  HashCode hashCode1 = HashCode.fromInt(1);

  Path tmp2 = Files.createTempDirectory("tmp2");
  ProjectFilesystem filesystem2 = new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp2);
  SourcePath input2 = PathSourcePath.of(filesystem2, Paths.get("input.h"));
  HashCode hashCode2 = HashCode.fromInt(1);

  FileHashLoader fileHashLoader =
      new FakeFileHashCache(
          ImmutableMap.of(
              RESOLVER.getAbsolutePath(input1),
              hashCode1,
              RESOLVER.getAbsolutePath(input2),
              hashCode2));

  Manifest manifest1 = new Manifest(new RuleKey("cc"));
  manifest1.addEntry(
      fileHashLoader, key, RESOLVER, ImmutableSet.of(input1, input2), ImmutableSet.of(input1));
  assertThat(
      ManifestUtil.toMap(manifest1),
      Matchers.equalTo(
          ImmutableMap.of(
              key,
              ImmutableMap.of(
                  RESOLVER.getRelativePath(input1).toString(),
                  Manifest.hashSourcePathGroup(
                      fileHashLoader, RESOLVER, ImmutableList.of(input1, input2))))));

  Manifest manifest2 = new Manifest(new RuleKey("cc"));
  manifest2.addEntry(
      fileHashLoader, key, RESOLVER, ImmutableSet.of(input1, input2), ImmutableSet.of(input2));
  assertThat(
      ManifestUtil.toMap(manifest2),
      Matchers.equalTo(
          ImmutableMap.of(
              key,
              ImmutableMap.of(
                  RESOLVER.getRelativePath(input2).toString(),
                  Manifest.hashSourcePathGroup(
                      fileHashLoader, RESOLVER, ImmutableList.of(input1, input2))))));
}
 
Example 13
Source File: ManifestTest.java    From buck with Apache License 2.0 4 votes vote down vote up
@Test
public void lookupMatchWithSourcePathsThatHaveSameRelativePaths() throws IOException {
  RuleKey key = new RuleKey("aa");

  Path tmp1 = Files.createTempDirectory("tmp1");
  ProjectFilesystem filesystem1 = new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp1);
  SourcePath input1 = PathSourcePath.of(filesystem1, Paths.get("input.h"));
  HashCode hashCode1 = HashCode.fromInt(1);

  Path tmp2 = Files.createTempDirectory("tmp2");
  ProjectFilesystem filesystem2 = new FakeProjectFilesystem(CanonicalCellName.rootCell(), tmp2);
  SourcePath input2 = PathSourcePath.of(filesystem2, Paths.get("input.h"));
  HashCode hashCode2 = HashCode.fromInt(1);

  FileHashLoader fileHashLoader =
      new FakeFileHashCache(
          ImmutableMap.of(
              RESOLVER.getAbsolutePath(input1),
              hashCode1,
              RESOLVER.getAbsolutePath(input2),
              hashCode2));

  Manifest manifest1 =
      ManifestUtil.fromMap(
          new RuleKey("cc"),
          ImmutableMap.of(
              key,
              ImmutableMap.of(
                  RESOLVER.getRelativePath(input1).toString(),
                  Manifest.hashSourcePathGroup(
                      fileHashLoader, RESOLVER, ImmutableList.of(input1, input2)))));
  assertThat(
      manifest1.lookup(fileHashLoader, RESOLVER, ImmutableSet.of(input1, input2)),
      Matchers.equalTo(Optional.of(key)));

  Manifest manifest2 =
      ManifestUtil.fromMap(
          new RuleKey("cc"),
          ImmutableMap.of(
              key,
              ImmutableMap.of(
                  RESOLVER.getRelativePath(input2).toString(),
                  Manifest.hashSourcePathGroup(
                      fileHashLoader, RESOLVER, ImmutableList.of(input1, input2)))));
  assertThat(
      manifest2.lookup(fileHashLoader, RESOLVER, ImmutableSet.of(input1, input2)),
      Matchers.equalTo(Optional.of(key)));
}