Skip to content

Commit

Permalink
[core] Fix checkstyle for CoreWorkload
Browse files Browse the repository at this point in the history
  • Loading branch information
risdenk committed Jan 18, 2017
1 parent 8b0548a commit 891da2c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import com.yahoo.ycsb.DBException;
import com.yahoo.ycsb.Status;

import static com.yahoo.ycsb.workloads.CoreWorkload.TABLENAME_PROPERTY;
import static com.yahoo.ycsb.workloads.CoreWorkload.TABLENAME_PROPERTY_DEFAULT;

/**
* Alternative Java client for Apache HBase.
*
Expand Down Expand Up @@ -140,7 +143,7 @@ public void init() throws DBException {
// Terminate right now if table does not exist, since the client
// will not propagate this error upstream once the workload
// starts.
String table = com.yahoo.ycsb.workloads.CoreWorkload.table;
String table = getProperties().getProperty(TABLENAME_PROPERTY, TABLENAME_PROPERTY_DEFAULT);
try {
client.ensureTableExists(table).join(joinTimeout);
} catch (InterruptedException e1) {
Expand Down
131 changes: 57 additions & 74 deletions core/src/main/java/com/yahoo/ycsb/workloads/CoreWorkload.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2010 Yahoo! Inc., Copyright (c) 2016 YCSB contributors. All rights reserved.
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
Expand All @@ -17,39 +17,18 @@

package com.yahoo.ycsb.workloads;

import java.util.Properties;

import com.yahoo.ycsb.*;
import com.yahoo.ycsb.generator.AcknowledgedCounterGenerator;
import com.yahoo.ycsb.generator.ConstantIntegerGenerator;
import com.yahoo.ycsb.generator.CounterGenerator;
import com.yahoo.ycsb.generator.DiscreteGenerator;
import com.yahoo.ycsb.generator.ExponentialGenerator;
import com.yahoo.ycsb.generator.HistogramGenerator;
import com.yahoo.ycsb.generator.HotspotIntegerGenerator;
import com.yahoo.ycsb.generator.NumberGenerator;
import com.yahoo.ycsb.generator.ScrambledZipfianGenerator;
import com.yahoo.ycsb.generator.SequentialGenerator;
import com.yahoo.ycsb.generator.SkewedLatestGenerator;
import com.yahoo.ycsb.generator.UniformIntegerGenerator;
import com.yahoo.ycsb.generator.ZipfianGenerator;
import com.yahoo.ycsb.generator.*;
import com.yahoo.ycsb.measurements.Measurements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Vector;

import java.util.*;

/**
* The core benchmark scenario. Represents a set of clients doing simple CRUD operations. The
* relative proportion of different kinds of operations, and other properties of the workload,
* are controlled by parameters specified at runtime.
*
* <p>
* Properties to control the client:
* <UL>
* <LI><b>fieldcount</b>: the number of fields in a record (default: 10)
Expand All @@ -75,7 +54,7 @@
* <LI><b>zeropadding</b>: for generating a record sequence compatible with string sort order by
* 0 padding the record number. Controls the number of 0s to use for padding. (default: 1)
* For example for row 5, with zeropadding=1 you get 'user5' key and with zeropading=8 you get
* 'user00000005' key. In order to see its impact, zeropadding needs to be bigger than number of
* 'user00000005' key. In order to see its impact, zeropadding needs to be bigger than number of
* digits in the record number.
* <LI><b>insertorder</b>: should records be inserted in order by key ("ordered"), or in hashed
* order ("hashed") (default: hashed)
Expand All @@ -92,8 +71,7 @@ public class CoreWorkload extends Workload {
*/
public static final String TABLENAME_PROPERTY_DEFAULT = "usertable";

public static String table;

private String table;

/**
* The name of the property for the number of fields in a record.
Expand All @@ -105,14 +83,14 @@ public class CoreWorkload extends Workload {
*/
public static final String FIELD_COUNT_PROPERTY_DEFAULT = "10";

int fieldcount;
private int fieldcount;

private List<String> fieldnames;

/**
* The name of the property for the field length distribution. Options are "uniform", "zipfian"
* (favouring short records), "constant", and "histogram".
*
* <p>
* If "uniform", "zipfian" or "constant", the maximum field length will be that specified by the
* fieldlength property. If "histogram", then the histogram will be read from the filename
* specified in the "fieldlengthhistogram" property.
Expand Down Expand Up @@ -149,7 +127,7 @@ public class CoreWorkload extends Workload {
* Generator object that produces field lengths. The value of this depends on the properties that
* start with "FIELD_LENGTH_".
*/
NumberGenerator fieldlengthgenerator;
private NumberGenerator fieldlengthgenerator;

/**
* The name of the property for deciding whether to read one field (false) or all fields (true) of
Expand All @@ -162,7 +140,7 @@ public class CoreWorkload extends Workload {
*/
public static final String READ_ALL_FIELDS_PROPERTY_DEFAULT = "true";

boolean readallfields;
private boolean readallfields;

/**
* The name of the property for deciding whether to write one field (false) or all fields (true)
Expand All @@ -175,8 +153,7 @@ public class CoreWorkload extends Workload {
*/
public static final String WRITE_ALL_FIELDS_PROPERTY_DEFAULT = "false";

boolean writeallfields;

private boolean writeallfields;

/**
* The name of the property for deciding whether to check all returned
Expand Down Expand Up @@ -256,7 +233,7 @@ public class CoreWorkload extends Workload {
*/
public static final String REQUEST_DISTRIBUTION_PROPERTY_DEFAULT = "uniform";

/**
/**
* The name of the property for adding zero padding to record numbers in order to match
* string sort order. Controls the number of 0s to left pad with.
*/
Expand All @@ -267,7 +244,7 @@ public class CoreWorkload extends Workload {
*/
public static final String ZERO_PADDING_PROPERTY_DEFAULT = "1";


/**
* The name of the property for the max scan length (number of records).
*/
Expand Down Expand Up @@ -331,29 +308,29 @@ public class CoreWorkload extends Workload {
public static final String INSERTION_RETRY_INTERVAL = "core_workload_insertion_retry_interval";
public static final String INSERTION_RETRY_INTERVAL_DEFAULT = "3";

NumberGenerator keysequence;
private NumberGenerator keysequence;

DiscreteGenerator operationchooser;
private DiscreteGenerator operationchooser;

NumberGenerator keychooser;
private NumberGenerator keychooser;

NumberGenerator fieldchooser;
private NumberGenerator fieldchooser;

AcknowledgedCounterGenerator transactioninsertkeysequence;
private AcknowledgedCounterGenerator transactioninsertkeysequence;

NumberGenerator scanlength;
private NumberGenerator scanlength;

boolean orderedinserts;
private boolean orderedinserts;

int recordcount;
int zeropadding;
private int recordcount;
private int zeropadding;

int insertionRetryLimit;
int insertionRetryInterval;
private int insertionRetryLimit;
private int insertionRetryInterval;

private Measurements _measurements = Measurements.getMeasurements();
private Measurements measurements = Measurements.getMeasurements();

protected static NumberGenerator getFieldLengthGenerator(Properties p) throws WorkloadException {
static NumberGenerator getFieldLengthGenerator(Properties p) throws WorkloadException {
NumberGenerator fieldlengthgenerator;
String fieldlengthdistribution = p.getProperty(
FIELD_LENGTH_DISTRIBUTION_PROPERTY, FIELD_LENGTH_DISTRIBUTION_PROPERTY_DEFAULT);
Expand Down Expand Up @@ -391,12 +368,12 @@ public void init(Properties p) throws WorkloadException {

fieldcount =
Integer.parseInt(p.getProperty(FIELD_COUNT_PROPERTY, FIELD_COUNT_PROPERTY_DEFAULT));
fieldnames = new ArrayList<String>();
fieldnames = new ArrayList<>();
for (int i = 0; i < fieldcount; i++) {
fieldnames.add("field" + i);
}
fieldlengthgenerator = CoreWorkload.getFieldLengthGenerator(p);

recordcount =
Integer.parseInt(p.getProperty(Client.RECORD_COUNT_PROPERTY, Client.DEFAULT_RECORD_COUNT));
if (recordcount == 0) {
Expand Down Expand Up @@ -432,8 +409,8 @@ public void init(Properties p) throws WorkloadException {
// Confirm that fieldlengthgenerator returns a constant if data
// integrity check requested.
if (dataintegrity && !(p.getProperty(
FIELD_LENGTH_DISTRIBUTION_PROPERTY,
FIELD_LENGTH_DISTRIBUTION_PROPERTY_DEFAULT)).equals("constant")) {
FIELD_LENGTH_DISTRIBUTION_PROPERTY,
FIELD_LENGTH_DISTRIBUTION_PROPERTY_DEFAULT)).equals("constant")) {
System.err.println("Must have constant field size to check data integrity.");
System.exit(-1);
}
Expand All @@ -460,7 +437,7 @@ public void init(Properties p) throws WorkloadException {
keychooser = new UniformIntegerGenerator(insertstart, insertstart + insertcount - 1);
} else if (requestdistrib.compareTo("sequential") == 0) {
keychooser = new SequentialGenerator(insertstart, insertstart + insertcount - 1);
}else if (requestdistrib.compareTo("zipfian") == 0) {
} else if (requestdistrib.compareTo("zipfian") == 0) {
// it does this by generating a random "next key" in part by taking the modulus over the
// number of keys.
// If the number of keys changes, this would shift the modulus, and we don't want that to
Expand Down Expand Up @@ -506,14 +483,14 @@ public void init(Properties p) throws WorkloadException {
INSERTION_RETRY_INTERVAL, INSERTION_RETRY_INTERVAL_DEFAULT));
}

public String buildKeyName(long keynum) {
private String buildKeyName(long keynum) {
if (!orderedinserts) {
keynum = Utils.hash(keynum);
}
String value = Long.toString(keynum);
int fill = zeropadding - value.length();
String prekey = "user";
for(int i=0; i<fill; i++) {
for (int i = 0; i < fill; i++) {
prekey += '0';
}
return prekey + value;
Expand All @@ -523,7 +500,7 @@ public String buildKeyName(long keynum) {
* Builds a value for a randomly chosen field.
*/
private HashMap<String, ByteIterator> buildSingleValue(String key) {
HashMap<String, ByteIterator> value = new HashMap<String, ByteIterator>();
HashMap<String, ByteIterator> value = new HashMap<>();

String fieldkey = fieldnames.get(fieldchooser.nextValue().intValue());
ByteIterator data;
Expand All @@ -542,7 +519,7 @@ private HashMap<String, ByteIterator> buildSingleValue(String key) {
* Builds values for all fields.
*/
private HashMap<String, ByteIterator> buildValues(String key) {
HashMap<String, ByteIterator> values = new HashMap<String, ByteIterator>();
HashMap<String, ByteIterator> values = new HashMap<>();

for (String fieldkey : fieldnames) {
ByteIterator data;
Expand Down Expand Up @@ -626,7 +603,12 @@ public boolean doInsert(DB db, Object threadstate) {
*/
@Override
public boolean doTransaction(DB db, Object threadstate) {
switch (operationchooser.nextString()) {
String operation = operationchooser.nextString();
if(operation == null) {
return false;
}

switch (operation) {
case "READ":
doTransactionRead(db);
break;
Expand All @@ -641,7 +623,7 @@ public boolean doTransaction(DB db, Object threadstate) {
break;
default:
doTransactionReadModifyWrite(db);
}
}

return true;
}
Expand All @@ -653,7 +635,7 @@ public boolean doTransaction(DB db, Object threadstate) {
* Bucket 1 means incorrect data was returned.
* Bucket 2 means null data was returned when some data was expected.
*/
protected void verifyRow(String key, HashMap<String, ByteIterator> cells) {
private void verifyRow(String key, HashMap<String, ByteIterator> cells) {
Status verifyStatus = Status.OK;
long startTime = System.nanoTime();
if (!cells.isEmpty()) {
Expand All @@ -668,11 +650,11 @@ protected void verifyRow(String key, HashMap<String, ByteIterator> cells) {
verifyStatus = Status.ERROR;
}
long endTime = System.nanoTime();
_measurements.measure("VERIFY", (int) (endTime - startTime) / 1000);
_measurements.reportStatus("VERIFY", verifyStatus);
measurements.measure("VERIFY", (int) (endTime - startTime) / 1000);
measurements.reportStatus("VERIFY", verifyStatus);
}

int nextKeynum() {
private int nextKeynum() {
int keynum;
if (keychooser instanceof ExponentialGenerator) {
do {
Expand Down Expand Up @@ -712,8 +694,8 @@ public void doTransactionRead(DB db) {
verifyRow(keyname, cells);
}
}
public void doTransactionReadModifyWrite(DB db) {

private void doTransactionReadModifyWrite(DB db) {
// choose a random key
int keynum = nextKeynum();

Expand Down Expand Up @@ -744,7 +726,7 @@ public void doTransactionReadModifyWrite(DB db) {
HashMap<String, ByteIterator> cells = new HashMap<String, ByteIterator>();


long ist = _measurements.getIntendedtartTimeNs();
long ist = measurements.getIntendedtartTimeNs();
long st = System.nanoTime();
db.read(table, keyname, fields, cells);

Expand All @@ -756,11 +738,11 @@ public void doTransactionReadModifyWrite(DB db) {
verifyRow(keyname, cells);
}

_measurements.measure("READ-MODIFY-WRITE", (int) ((en - st) / 1000));
_measurements.measureIntended("READ-MODIFY-WRITE", (int) ((en - ist) / 1000));
measurements.measure("READ-MODIFY-WRITE", (int) ((en - st) / 1000));
measurements.measureIntended("READ-MODIFY-WRITE", (int) ((en - ist) / 1000));
}

public void doTransactionScan(DB db) {
private void doTransactionScan(DB db) {
// choose a random key
int keynum = nextKeynum();

Expand Down Expand Up @@ -820,11 +802,12 @@ public void doTransactionInsert(DB db) {
* Weights/proportions are read from the properties list and defaults are used
* when values are not configured.
* Current operations are "READ", "UPDATE", "INSERT", "SCAN" and "READMODIFYWRITE".
*
* @param p The properties list to pull weights from.
* @return A generator that can be used to determine the next operation to perform.
* @throws IllegalArgumentException if the properties object was null.
*/
public static DiscreteGenerator createOperationGenerator(final Properties p) {
static DiscreteGenerator createOperationGenerator(final Properties p) {
if (p == null) {
throw new IllegalArgumentException("Properties object cannot be null");
}
Expand All @@ -838,7 +821,7 @@ public static DiscreteGenerator createOperationGenerator(final Properties p) {
p.getProperty(SCAN_PROPORTION_PROPERTY, SCAN_PROPORTION_PROPERTY_DEFAULT));
final double readmodifywriteproportion = Double.parseDouble(p.getProperty(
READMODIFYWRITE_PROPORTION_PROPERTY, READMODIFYWRITE_PROPORTION_PROPERTY_DEFAULT));

final DiscreteGenerator operationchooser = new DiscreteGenerator();
if (readproportion > 0) {
operationchooser.addValue(readproportion, "READ");
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/com/yahoo/ycsb/workloads/RestWorkload.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ public class RestWorkload extends CoreWorkload {
private NumberGenerator insertKeyChooser;
private NumberGenerator deleteKeyChooser;
private NumberGenerator updateKeyChooser;
private NumberGenerator fieldlengthgenerator;
private DiscreteGenerator operationchooser;

@Override
public void init(Properties p) throws WorkloadException {
Expand Down

0 comments on commit 891da2c

Please sign in to comment.