ONOS-2088: Lazy id block allocation
Change-Id: Ie99d2ba017f45f1ca99b04da05cdb06b41202d3a
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..ca483b4 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
@@ -15,6 +15,8 @@
*/
package org.onosproject.core.impl;
+import java.util.concurrent.atomic.AtomicBoolean;
+
import org.onosproject.core.IdBlock;
import org.onosproject.core.IdGenerator;
import org.onosproject.core.UnavailableIdException;
@@ -26,6 +28,8 @@
public class BlockAllocatorBasedIdGenerator implements IdGenerator {
protected final IdBlockAllocator allocator;
protected IdBlock idBlock;
+ protected AtomicBoolean initialized;
+
/**
* Constructs an ID generator which use {@link IdBlockAllocator} as backend.
@@ -34,18 +38,26 @@
*/
protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) {
this.allocator = allocator;
- this.idBlock = allocator.allocateUniqueIdBlock();
+ this.initialized = new AtomicBoolean(false);
}
@Override
public long getNewId() {
try {
+ if (!initialized.get()) {
+ synchronized (allocator) {
+ if (!initialized.get()) {
+ idBlock = allocator.allocateUniqueIdBlock();
+ initialized.set(true);
+ }
+ }
+ }
return idBlock.getNextId();
} catch (UnavailableIdException e) {
synchronized (allocator) {
idBlock = allocator.allocateUniqueIdBlock();
- return idBlock.getNextId();
}
+ return idBlock.getNextId();
}
}
}