// Copyright 2016 The Nomulus Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package google.registry.model.ofy; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.DiscreteDomain.integers; import static com.googlecode.objectify.ObjectifyService.ofy; import static google.registry.util.DateTimeUtils.START_OF_TIME; import com.google.common.base.Function; import com.google.common.base.Supplier; import com.google.common.collect.ContiguousSet; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSortedSet; import com.google.common.collect.Range; import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Id; import google.registry.config.RegistryEnvironment; import google.registry.model.Buildable; import google.registry.model.ImmutableObject; import google.registry.model.annotations.NotBackedUp; import google.registry.model.annotations.NotBackedUp.Reason; import google.registry.util.NonFinalForTesting; import java.util.Random; import org.joda.time.DateTime; /** * Root for a random commit log bucket. * *
This is used to shard {@link CommitLogManifest} objects into {@link * google.registry.config.RegistryConfig#getCommitLogBucketCount() N} entity groups. This increases * transaction throughput, while maintaining the ability to perform strongly-consistent ancestor * queries. * * @see Avoiding datastore * contention */ @Entity @NotBackedUp(reason = Reason.COMMIT_LOGS) public class CommitLogBucket extends ImmutableObject implements Buildable { private static final RegistryEnvironment ENVIRONMENT = RegistryEnvironment.get(); /** Ranges from 1 to {@link #getNumBuckets()}, inclusive; starts at 1 since IDs can't be 0. */ @Id long bucketNum; /** The timestamp of the last {@link CommitLogManifest} written to this bucket. */ DateTime lastWrittenTime = START_OF_TIME; public int getBucketNum() { return (int) bucketNum; } public DateTime getLastWrittenTime() { return lastWrittenTime; } /** * Returns the key for the specified bucket ID. * *
Always use this method in preference to manually creating bucket keys, since manual keys
* are not guaranteed to have a valid bucket ID number.
*/
public static Key Default supplier is one that returns bucket IDs via uniform random selection, but can be
* overridden in tests that rely on predictable bucket assignment for commit logs.
*/
@NonFinalForTesting
private static Supplier