Skip to content

Commit

Permalink
[Core] Add retry for insertion logic to make the load process more ro…
Browse files Browse the repository at this point in the history
…bust

The default behavior is NO RETRY, which is the current behavior.

But optionally user can enable retry if they are working with a DB backend that is better tolerated by allowing a few retries.
  • Loading branch information
stfeng2 committed Dec 12, 2015
1 parent ed0a318 commit ad8404a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
54 changes: 49 additions & 5 deletions core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ public class CoreWorkload extends Workload {
*/
public static final String HOTSPOT_OPN_FRACTION_DEFAULT = "0.8";

/**
* How many times to retry when insertion of a single item to a DB fails.
*/
public static final String INSERTION_RETRY_LIMIT_DEFAULT = "0";

/**
* On average, how long to wait between the retries, in seconds.
*/
public static final String INSERTION_RETRY_INTERVAL_DEFAULT = "3";

IntegerGenerator keysequence;

DiscreteGenerator operationchooser;
Expand All @@ -312,6 +322,9 @@ public class CoreWorkload extends Workload {

int recordcount;

int insertionRetryLimit;
int insertionRetryInterval;

private Measurements _measurements = Measurements.getMeasurements();

protected static IntegerGenerator getFieldLengthGenerator(Properties p) throws WorkloadException {
Expand Down Expand Up @@ -475,6 +488,12 @@ public void init(Properties p) throws WorkloadException {
throw new WorkloadException(
"Distribution \"" + scanlengthdistrib + "\" not allowed for scan length");
}

insertionRetryLimit = Integer.parseInt(p.getProperty(
"insertionretrylimit", INSERTION_RETRY_LIMIT_DEFAULT));

insertionRetryInterval = Integer.parseInt(p.getProperty(
"insertionretryinterval", INSERTION_RETRY_INTERVAL_DEFAULT));
}

public String buildKeyName(long keynum) {
Expand Down Expand Up @@ -550,12 +569,37 @@ public boolean doInsert(DB db, Object threadstate) {
int keynum = keysequence.nextInt();
String dbkey = buildKeyName(keynum);
HashMap<String, ByteIterator> values = buildValues(dbkey);
if (db.insert(table, dbkey, values).equals(Status.OK))
return true;
else
return false;
}

Status status;
int numOfRetries = 0;
do {
status = db.insert(table, dbkey, values);
if (status == Status.OK) {
break;
}
// Retry if configured. Without retrying, the load process will fail
// even if one single insertion fails. User can optionally configure
// an insertion retry limit (default is 0) to enable retry.
if (++numOfRetries <= insertionRetryLimit) {
System.err.println("Retrying insertion, retry count: " + numOfRetries);
try {
// Sleep for a random number between [0.8, 1.2)*insertionRetryInterval.
int sleepTime = (int) (1000 * insertionRetryInterval * (0.8 + 0.4 * Math.random()));
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
break;
}

} else {
System.err.println("Error inserting, not retrying any more. number of attempts: " + numOfRetries +
"Insertion Retry Limit: " + insertionRetryLimit);
break;

}
} while (true);

return (status == Status.OK);
}

/**
* Do one transaction operation. Because it will be called concurrently from multiple client
Expand Down
12 changes: 12 additions & 0 deletions workloads/workload_template
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,15 @@ timeseries.granularity=1000
# property.
# reportlatencyforeacherror=false
# latencytrackederrors="<comma separated strings of error codes>"

# Insertion error retry
#
# By default, YCSB does not retry any operations. However, during the
# load process, if any insertion fails, the entire load process is terminated.
# If a user desires to have more robust behavior during this phase, they can
# enable retry for insertion by setting the following property to a positive
# number.
# insertionretrylimit = 0
#
# the following number controls the interval between retries (in seconds):
# insertionretryinterval = 3

0 comments on commit ad8404a

Please sign in to comment.