ONOS-1909: Defer id block allocation to the point when they are actually needed
Change-Id: Id34cba5259ae67b81df2480072ea0ce5c2417075
Showing
1 changed file
with
8 additions
and
5 deletions
| ... | @@ -19,13 +19,16 @@ import org.onosproject.core.IdBlock; | ... | @@ -19,13 +19,16 @@ import org.onosproject.core.IdBlock; |
| 19 | import org.onosproject.core.IdGenerator; | 19 | import org.onosproject.core.IdGenerator; |
| 20 | import org.onosproject.core.UnavailableIdException; | 20 | import org.onosproject.core.UnavailableIdException; |
| 21 | 21 | ||
| 22 | +import com.google.common.base.Supplier; | ||
| 23 | +import com.google.common.base.Suppliers; | ||
| 24 | + | ||
| 22 | /** | 25 | /** |
| 23 | * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as | 26 | * Base class of {@link IdGenerator} implementations which use {@link IdBlockAllocator} as |
| 24 | * backend. | 27 | * backend. |
| 25 | */ | 28 | */ |
| 26 | public class BlockAllocatorBasedIdGenerator implements IdGenerator { | 29 | public class BlockAllocatorBasedIdGenerator implements IdGenerator { |
| 27 | protected final IdBlockAllocator allocator; | 30 | protected final IdBlockAllocator allocator; |
| 28 | - protected IdBlock idBlock; | 31 | + protected Supplier<IdBlock> idBlock; |
| 29 | 32 | ||
| 30 | /** | 33 | /** |
| 31 | * Constructs an ID generator which use {@link IdBlockAllocator} as backend. | 34 | * Constructs an ID generator which use {@link IdBlockAllocator} as backend. |
| ... | @@ -34,17 +37,17 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator { | ... | @@ -34,17 +37,17 @@ public class BlockAllocatorBasedIdGenerator implements IdGenerator { |
| 34 | */ | 37 | */ |
| 35 | protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) { | 38 | protected BlockAllocatorBasedIdGenerator(IdBlockAllocator allocator) { |
| 36 | this.allocator = allocator; | 39 | this.allocator = allocator; |
| 37 | - this.idBlock = allocator.allocateUniqueIdBlock(); | 40 | + this.idBlock = Suppliers.memoize(allocator::allocateUniqueIdBlock); |
| 38 | } | 41 | } |
| 39 | 42 | ||
| 40 | @Override | 43 | @Override |
| 41 | public long getNewId() { | 44 | public long getNewId() { |
| 42 | try { | 45 | try { |
| 43 | - return idBlock.getNextId(); | 46 | + return idBlock.get().getNextId(); |
| 44 | } catch (UnavailableIdException e) { | 47 | } catch (UnavailableIdException e) { |
| 45 | synchronized (allocator) { | 48 | synchronized (allocator) { |
| 46 | - idBlock = allocator.allocateUniqueIdBlock(); | 49 | + idBlock = Suppliers.memoize(allocator::allocateUniqueIdBlock); |
| 47 | - return idBlock.getNextId(); | 50 | + return idBlock.get().getNextId(); |
| 48 | } | 51 | } |
| 49 | } | 52 | } |
| 50 | } | 53 | } | ... | ... |
-
Please register or login to post a comment