ONOS-1909: Defer id block allocation to the point when they are actually needed
Change-Id: Id34cba5259ae67b81df2480072ea0ce5c2417075
diff --git a/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java b/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java
index 53b70d5..42d397f 100644
--- a/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java
+++ b/core/net/src/main/java/org/onosproject/core/impl/BlockAllocatorBasedIdGenerator.java
@@ -19,13 +19,16 @@
import org.onosproject.core.IdGenerator;
import org.onosproject.core.UnavailableIdException;
+import com.google.common.base.Supplier;
+import com.google.common.base.Suppliers;
+
/**
* Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as
* backend.
*/
public class BlockAllocatorBasedIdGenerator implements IdGenerator {
protected final IdBlockAllocator allocator;
- protected IdBlock idBlock;
+ protected Supplier<IdBlock> idBlock;
/**
* Constructs an ID generator which use {@link IdBlockAllocator} as backend.
@@ -34,17 +37,17 @@
*/
protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
this.allocator = allocator;
- this.idBlock = allocator.allocateUniqueIdBlock();
+ this.idBlock = Suppliers.memoize(allocator::allocateUniqueIdBlock);
}
@Override
public long getNewId() {
try {
- return idBlock.getNextId();
+ return idBlock.get().getNextId();
} catch (UnavailableIdException e) {
synchronized (allocator) {
- idBlock = allocator.allocateUniqueIdBlock();
- return idBlock.getNextId();
+ idBlock = Suppliers.memoize(allocator::allocateUniqueIdBlock);
+ return idBlock.get().getNextId();
}
}
}