Skip to content

Commit

Permalink
[kudu] use slf4j and log4j for logging
Browse files Browse the repository at this point in the history
  • Loading branch information
danburkert committed Dec 2, 2016
1 parent 23a3780 commit cacf13b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 36 deletions.
10 changes: 10 additions & 0 deletions kudu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ LICENSE file.
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
</project>
51 changes: 15 additions & 36 deletions kudu/src/main/java/com/yahoo/ycsb/db/KuduYCSBClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.yahoo.ycsb.Status;
import com.yahoo.ycsb.StringByteIterator;
import com.yahoo.ycsb.workloads.CoreWorkload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.client.*;
Expand Down Expand Up @@ -58,41 +61,27 @@
* </blockquote>
*/
public class KuduYCSBClient extends com.yahoo.ycsb.DB {
private static final Logger LOG = LoggerFactory.getLogger(KuduYCSBClient.class);
private static final String KEY = "key";
private static final Status TIMEOUT = new Status("TIMEOUT", "The operation timed out.");
private static final int MAX_TABLETS = 9000;
private static final long DEFAULT_SLEEP = 60000;
private static final String SYNC_OPS_OPT = "kudu_sync_ops";
private static final String DEBUG_OPT = "kudu_debug";
private static final String PRINT_ROW_ERRORS_OPT = "kudu_print_row_errors";
private static final String PRE_SPLIT_NUM_TABLETS_OPT = "kudu_pre_split_num_tablets";
private static final String TABLE_NUM_REPLICAS = "kudu_table_num_replicas";
private static final String BLOCK_SIZE_OPT = "kudu_block_size";
private static final String MASTER_ADDRESSES_OPT = "kudu_master_addresses";
private static final int BLOCK_SIZE_DEFAULT = 4096;
private static final List<String> COLUMN_NAMES = new ArrayList<String>();
private static final List<String> COLUMN_NAMES = new ArrayList<>();
private static KuduClient client;
private static Schema schema;
private boolean debug = false;
private boolean printErrors = false;
private KuduSession session;
private KuduTable kuduTable;

@Override
public void init() throws DBException {
if (getProperties().getProperty(DEBUG_OPT) != null) {
this.debug = getProperties().getProperty(DEBUG_OPT).equals("true");
}
if (getProperties().getProperty(PRINT_ROW_ERRORS_OPT) != null) {
this.printErrors =
getProperties().getProperty(PRINT_ROW_ERRORS_OPT).equals("true");
}
if (getProperties().getProperty(PRINT_ROW_ERRORS_OPT) != null) {
this.printErrors =
getProperties().getProperty(PRINT_ROW_ERRORS_OPT).equals("true");
}
String tableName = CoreWorkload.table;
initClient(debug, tableName, getProperties());
initClient(tableName, getProperties());
this.session = client.newSession();
if (getProperties().getProperty(SYNC_OPS_OPT) != null
&& getProperties().getProperty(SYNC_OPS_OPT).equals("false")) {
Expand All @@ -109,8 +98,7 @@ && getProperties().getProperty(SYNC_OPS_OPT).equals("false")) {
}
}

private static synchronized void initClient(boolean debug,
String tableName,
private static synchronized void initClient(String tableName,
Properties prop) throws DBException {
if (client != null) {
return;
Expand All @@ -135,9 +123,7 @@ private static synchronized void initClient(boolean debug,
.defaultOperationTimeoutMs(DEFAULT_SLEEP)
.defaultAdminOperationTimeoutMs(DEFAULT_SLEEP)
.build();
if (debug) {
System.out.println("Connecting to the masters at " + masterAddresses);
}
LOG.debug("Connecting to the masters at {}", masterAddresses);

int fieldCount = getIntFromProp(prop, CoreWorkload.FIELD_COUNT_PROPERTY,
Integer.parseInt(CoreWorkload.FIELD_COUNT_PROPERTY_DEFAULT));
Expand Down Expand Up @@ -251,13 +237,10 @@ public Status scan(String table,
RowResultIterator closer = scanner.close();
addAllRowsToResult(closer, recordcount, querySchema, result);
} catch (TimeoutException te) {
if (printErrors) {
System.err.println("Waited too long for a scan operation with start key=" + startkey);
}
LOG.info("Waited too long for a scan operation with start key={}", startkey);
return TIMEOUT;
} catch (Exception e) {
System.err.println("Unexpected exception " + e);
e.printStackTrace();
LOG.warn("Unexpected exception", e);
return Status.ERROR;
}
return Status.OK;
Expand Down Expand Up @@ -308,8 +291,7 @@ public Status insert(String table, String key, HashMap<String, ByteIterator> val
PartialRow row = insert.getRow();
row.addString(KEY, key);
for (int i = 1; i < schema.getColumnCount(); i++) {
row.addString(i, new String(
values.get(schema.getColumnByIndex(i).getName()).toArray()));
row.addString(i, values.get(schema.getColumnByIndex(i).getName()).toString());
}
apply(insert);
return Status.OK;
Expand All @@ -327,14 +309,11 @@ public Status delete(String table, String key) {
private void apply(Operation op) {
try {
OperationResponse response = session.apply(op);
if (response != null && response.hasRowError() && printErrors) {
System.err.println("Got a row error " + response.getRowError());
}
} catch (Exception ex) {
if (printErrors) {
System.err.println("Failed to apply an operation " + ex.toString());
ex.printStackTrace();
if (response != null && response.hasRowError()) {
LOG.info("Write operation failed: {}", response.getRowError());
}
} catch (KuduException ex) {
LOG.warn("Write operation failed", ex);
}
}
}
11 changes: 11 additions & 0 deletions kudu/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Root logger option
log4j.rootLogger=DEBUG, stderr

log4j.logger.com.stumbleupon.async=WARN
log4j.logger.org.apache.kudu=WARN

# Direct log messages to stderr
log4j.appender.stderr = org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout = org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern = [%p] %d{HH:mm:ss.SSS} (%F:%L) %m%n

0 comments on commit cacf13b

Please sign in to comment.