Skip to content

Commit 84b9b68

Browse files
committed
Resolve Merge conflicts
2 parents 6c22254 + 13d5407 commit 84b9b68

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

krystal-common/src/main/java/com/flipkart/krystal/pooling/PartitionedPool.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ private void tryMakeAvailable(int indexToMakeAvailable) {
163163

164164
PooledObject<T> toMove = partitionedList.get(indexToMakeAvailable);
165165
PooledObject<T> other = partitionedList.get(unavailableStartIndex);
166-
partitionedList.set(indexToMakeAvailable, other);
167-
partitionedList.set(unavailableStartIndex, toMove);
168166
toMove.index(unavailableStartIndex);
169167
other.index(indexToMakeAvailable);
168+
partitionedList.set(other.index(), other);
169+
partitionedList.set(toMove.index(), toMove);
170170
this.unavailableStartIndex++;
171171
}
172172

@@ -180,23 +180,21 @@ private void tryMakeUnavailable(int indexToMakeUnavailable) {
180180
indexToMakeUnavailable < partitionedList.size(),
181181
"Index to make unavailable should be < elements.size()");
182182

183-
final int unavailableStartIndex = this.unavailableStartIndex;
184-
185183
if (indexToMakeUnavailable >= unavailableStartIndex) {
186184
// This index is already unavailable!
187185
return;
188186
}
187+
final int newUnavailableStartIndex = this.unavailableStartIndex - 1;
189188
if (partitionedList.get(indexToMakeUnavailable).activeLeases < hardMaxLeasesPerObject) {
190189
// Object at index has not reached the hard max leases yet. So it cannot be made unavailable
191190
return;
192191
}
193192
PooledObject<T> toMove = partitionedList.get(indexToMakeUnavailable);
194-
int otherIndex = unavailableStartIndex - 1;
195-
PooledObject<T> other = partitionedList.get(otherIndex);
196-
partitionedList.set(indexToMakeUnavailable, other);
197-
partitionedList.set(otherIndex, toMove);
198-
toMove.index(unavailableStartIndex);
193+
PooledObject<T> other = partitionedList.get(newUnavailableStartIndex);
199194
other.index(indexToMakeUnavailable);
195+
toMove.index(newUnavailableStartIndex);
196+
partitionedList.set(other.index(), other);
197+
partitionedList.set(toMove.index(), toMove);
200198
this.unavailableStartIndex--;
201199
}
202200

krystal-common/src/test/java/com/flipkart/krystal/pooling/PartitionedPoolTest.java

+37
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44

55
import com.flipkart.krystal.pooling.PartitionedPool.PooledObject;
6+
import java.util.ArrayList;
7+
import java.util.List;
68
import org.junit.jupiter.api.Test;
79

810
class PartitionedPoolTest {
@@ -91,4 +93,39 @@ void closeLease_multipleObjects_makesObjectAvailable() {
9193
assertThat(partitionedPool.availableCount()).isEqualTo(i + 1);
9294
}
9395
}
96+
97+
@Test
98+
void leaseAndCloseLoop() throws LeaseUnavailableException {
99+
PartitionedPool<Object> partitionedPool = new PartitionedPool<>(1);
100+
101+
assertThat(partitionedPool.availableCount()).isEqualTo(0);
102+
for (int i = 0; i < 10; i++) {
103+
partitionedPool.closeLease(partitionedPool.leaseAndAdd(new Object()));
104+
}
105+
assertThat(partitionedPool.availableCount()).isEqualTo(10);
106+
107+
for (int i = 0; i < 10; i++) {
108+
PooledObject<Object> leasedObject = partitionedPool.getForLeasing(0);
109+
assertThat(partitionedPool.availableCount()).isEqualTo(9);
110+
partitionedPool.closeLease(leasedObject);
111+
assertThat(partitionedPool.availableCount()).isEqualTo(10);
112+
}
113+
}
114+
115+
@Test
116+
void leaseLoopAndCloseLoop() throws LeaseUnavailableException {
117+
PartitionedPool<Object> partitionedPool = new PartitionedPool<>(1);
118+
119+
List<PooledObject<Object>> leasedObjects = new ArrayList<>();
120+
121+
for (int i = 0; i < 10; i++) {
122+
leasedObjects.add(partitionedPool.leaseAndAdd(new Object()));
123+
assertThat(partitionedPool.availableCount()).isEqualTo(0);
124+
}
125+
for (int i = 0; i < 10; i++) {
126+
assertThat(partitionedPool.availableCount()).isEqualTo(i);
127+
partitionedPool.closeLease(leasedObjects.get(i));
128+
assertThat(partitionedPool.availableCount()).isEqualTo(i + 1);
129+
}
130+
}
94131
}

0 commit comments

Comments
 (0)