-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
419 additions
and
8 deletions.
There are no files selected for viewing
72 changes: 70 additions & 2 deletions
72
ingest/src/main/java/com/microsoft/azure/kusto/ingest/resources/RankedStorageAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
ingest/src/main/java/com/microsoft/azure/kusto/ingest/utils/DefaultRandomProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.microsoft.azure.kusto.ingest.utils; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class DefaultRandomProvider implements RandomProvider { | ||
public Random random; | ||
|
||
public DefaultRandomProvider(Random random) { | ||
this.random = random; | ||
} | ||
|
||
public DefaultRandomProvider() { | ||
this.random = new Random(); | ||
} | ||
|
||
@Override | ||
public void shuffle(List<?> list) { | ||
Collections.shuffle(list, random); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ingest/src/main/java/com/microsoft/azure/kusto/ingest/utils/RandomProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.microsoft.azure.kusto.ingest.utils; | ||
|
||
import java.util.List; | ||
|
||
public interface RandomProvider { | ||
void shuffle(List<?> list); | ||
} |
8 changes: 8 additions & 0 deletions
8
ingest/src/main/java/com/microsoft/azure/kusto/ingest/utils/SystemTimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.microsoft.azure.kusto.ingest.utils; | ||
|
||
public class SystemTimeProvider implements TimeProvider { | ||
@Override | ||
public long currentTimeMillis() { | ||
return System.currentTimeMillis(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
ingest/src/main/java/com/microsoft/azure/kusto/ingest/utils/TimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.microsoft.azure.kusto.ingest.utils; | ||
|
||
public interface TimeProvider { | ||
long currentTimeMillis(); | ||
} |
21 changes: 21 additions & 0 deletions
21
ingest/src/test/java/com/microsoft/azure/kusto/ingest/MockTimeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.microsoft.azure.kusto.ingest; | ||
|
||
import com.microsoft.azure.kusto.ingest.utils.TimeProvider; | ||
|
||
public class MockTimeProvider implements TimeProvider { | ||
private long currentTimeMillis; | ||
|
||
public MockTimeProvider(long currentTimeMillis) { | ||
this.currentTimeMillis = currentTimeMillis; | ||
} | ||
|
||
@Override | ||
public long currentTimeMillis() { | ||
return currentTimeMillis; | ||
} | ||
|
||
public void setCurrentTimeMillis(long currentTimeMillis) { | ||
this.currentTimeMillis = currentTimeMillis; | ||
} | ||
|
||
} |
120 changes: 120 additions & 0 deletions
120
...src/test/java/com/microsoft/azure/kusto/ingest/resources/RankedStorageAccountSetTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.microsoft.azure.kusto.ingest.resources; | ||
|
||
|
||
import com.microsoft.azure.kusto.ingest.MockTimeProvider; | ||
import com.microsoft.azure.kusto.ingest.utils.RandomProvider; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class RankedStorageAccountSetTest { | ||
|
||
class ByNameReverseOrderRandomProvider implements RandomProvider { | ||
@Override | ||
public void shuffle(List<?> list) { | ||
list.sort((o1, o2) -> ((RankedStorageAccount) o2).getAccountName().compareTo(((RankedStorageAccount) o1).getAccountName())); | ||
} | ||
} | ||
|
||
@Test | ||
public void testShuffledAccountsNoTiers() { | ||
int[] ints = new int[]{0}; | ||
|
||
RankedStorageAccountSet rankedStorageAccountSet = new RankedStorageAccountSet( | ||
6, | ||
10, | ||
ints, | ||
new MockTimeProvider(System.currentTimeMillis()), | ||
new ByNameReverseOrderRandomProvider()); | ||
|
||
rankedStorageAccountSet.addAccount("aSuccessful"); | ||
rankedStorageAccountSet.addAccount("bFailed"); | ||
rankedStorageAccountSet.addAccount("cHalf"); | ||
|
||
rankedStorageAccountSet.addResultToAccount("aSuccessful", true); | ||
rankedStorageAccountSet.addResultToAccount("bFailed", false); | ||
rankedStorageAccountSet.addResultToAccount("cHalf", true); | ||
rankedStorageAccountSet.addResultToAccount("cHalf", false); | ||
|
||
List<RankedStorageAccount> accounts = rankedStorageAccountSet.getRankedShuffledAccounts(); | ||
assertEquals("cHalf", accounts.get(0).getAccountName()); | ||
assertEquals("bFailed", accounts.get(1).getAccountName()); | ||
assertEquals("aSuccessful", accounts.get(2).getAccountName()); | ||
} | ||
|
||
@Test | ||
public void testShuffledAccounts() { | ||
int[] tiers = new int[]{90, 70, 30, 0}; | ||
|
||
RankedStorageAccountSet rankedStorageAccountSet = new RankedStorageAccountSet( | ||
6, | ||
10, | ||
tiers, | ||
new MockTimeProvider(System.currentTimeMillis()), | ||
new ByNameReverseOrderRandomProvider()); | ||
|
||
int[] values = new int[] {95, 40, 80, 20, 97, 10, 50, 75, 29, 0}; | ||
for (int i = 0; i < values.length; i++) { | ||
String name = String.format("%s%d", (char)('a' + i), values[i]); | ||
rankedStorageAccountSet.addAccount(name); | ||
for (int j = 0; j < 100; j++) { | ||
rankedStorageAccountSet.addResultToAccount(name, j < values[i]); | ||
} | ||
} | ||
|
||
String[][] expected = new String[][] { | ||
{"e97", "a95"}, | ||
{"h75", "c80"}, | ||
{"g50", "b40"}, | ||
{"j0", "i29", "f10", "d20"} | ||
}; | ||
|
||
List<RankedStorageAccount> accounts = rankedStorageAccountSet.getRankedShuffledAccounts(); | ||
int total = 0; | ||
for (String[] strings : expected) { | ||
for (int j = 0; j < strings.length; j++) { | ||
assertEquals(strings[j], accounts.get(total + j).getAccountName()); | ||
} | ||
total += strings.length; | ||
} | ||
} | ||
|
||
@Test | ||
public void testShuffledAccountsEmptyTier() { | ||
int[] tiers = new int[]{90, 70, 30, 0}; | ||
|
||
RankedStorageAccountSet rankedStorageAccountSet = new RankedStorageAccountSet( | ||
6, | ||
10, | ||
tiers, | ||
new MockTimeProvider(System.currentTimeMillis()), | ||
new ByNameReverseOrderRandomProvider()); | ||
|
||
int[] values = new int[] {95, 40, 20, 97, 10, 50}; | ||
for (int i = 0; i < values.length; i++) { | ||
String name = String.format("%s%d", (char)('a' + i), values[i]); | ||
rankedStorageAccountSet.addAccount(name); | ||
for (int j = 0; j < 100; j++) { | ||
rankedStorageAccountSet.addResultToAccount(name, j < values[i]); | ||
} | ||
} | ||
|
||
String[][] expected = new String[][] { | ||
{"d97", "a95"}, | ||
{"f50", "b40"}, | ||
{"e10", "c20"} | ||
}; | ||
|
||
List<RankedStorageAccount> accounts = rankedStorageAccountSet.getRankedShuffledAccounts(); | ||
int total = 0; | ||
for (String[] strings : expected) { | ||
for (int j = 0; j < strings.length; j++) { | ||
assertEquals(strings[j], accounts.get(total + j).getAccountName()); | ||
} | ||
total += strings.length; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.