Skip to content

Commit d2643ef

Browse files
committed
Minor Performance Improvements
- Remove expensive log formatting - Move from Exchanger that requires both produce and consumer to bet at the same point to a array based approach where the producer doesn't need to wait for the consumer - Added some more perfLogger output - Added extra VM option to output config - Updated FV tests that weren't thread sage Signed-off-by: Matthew B White <whitemat@uk.ibm.com>
1 parent 703558c commit d2643ef

File tree

13 files changed

+324
-303
lines changed

13 files changed

+324
-303
lines changed

fabric-chaincode-docker/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ RUN gradle \
4040
fabric-chaincode-shim:publishToMavenLocal \
4141
-x javadoc \
4242
-x test \
43-
-x checkstyleMain -x checkstyleTest
43+
-x checkstyleMain \
44+
-x checkstyleTest \
45+
-x dependencyCheckAnalyze
4446

4547
# Installing all protos jar dependencies to maven local
4648
WORKDIR /root/chaincode-java/shim-src/fabric-chaincode-protos/build/publications/protosJar/

fabric-chaincode-docker/start

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ LIB_JARS=$(find ${LIB_DIR} -name "*.jar" | paste -s -d ":" -)
88
CHAINCODE_JARS=$(find ${CHAINCODE_DIR} -name "*.jar" | paste -s -d ":" -)
99
NUM_CHAINCODE_JARS=$(find ${CHAINCODE_DIR} -name "*.jar" | wc -l)
1010

11+
JAVA_OPTS="-XshowSettings"
12+
1113
if [ -f ${CHAINCODE_DIR}/.uberjar ]; then
1214
if [ ${NUM_CHAINCODE_JARS} -ne 1 ]; then
1315
>&2 echo "Cannot start uber JAR as more than one JAR file was found in the chaincode directory"
1416
exit 255
1517
fi
16-
exec java -jar ${CHAINCODE_JARS} "$@"
18+
exec java ${JAVA_OPTS} -jar ${CHAINCODE_JARS} "$@"
1719
else
18-
exec java -cp ${CHAINCODE_JARS}:${LIB_JARS} org.hyperledger.fabric.contract.ContractRouter "$@"
20+
exec java ${JAVA_OPTS} -cp ${CHAINCODE_JARS}:${LIB_JARS} org.hyperledger.fabric.contract.ContractRouter "$@"
1921
fi

fabric-chaincode-protos/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ buildscript {
4747
dependencies {
4848
compile 'com.google.protobuf:protobuf-java:3.11.1'
4949
compile 'com.google.protobuf:protobuf-java-util:3.11.1'
50-
compile 'io.grpc:grpc-netty:1.30.2'
51-
compile 'io.grpc:grpc-protobuf:1.30.2'
52-
compile 'io.grpc:grpc-stub:1.30.2'
50+
compile 'io.grpc:grpc-netty-shaded:1.31.1'
51+
compile 'io.grpc:grpc-protobuf:1.31.1'
52+
compile 'io.grpc:grpc-stub:1.31.1'
5353
// Required if using Java 11+ as no longer bundled in the core libraries
5454
compile 'javax.annotation:javax.annotation-api:1.3.2'
55+
compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
5556
}
5657

5758
protobuf {

fabric-chaincode-shim/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ plugins {
2222

2323
apply plugin: 'org.owasp.dependencycheck'
2424

25-
2625
checkstyle {
2726
toolVersion '8.29'
2827
configFile file("../ci/checkstyle/checkstyle.xml")
@@ -37,7 +36,6 @@ checkstyleTest {
3736

3837

3938

40-
4139
check.dependsOn dependencyCheckAnalyze
4240

4341
tasks.withType(org.gradle.api.tasks.testing.Test) {
@@ -53,6 +51,9 @@ dependencies {
5351
compile group: 'org.json', name: 'json', version: '20180813'
5452
// Required if using Java 11+ as no longer bundled in the core libraries
5553
testCompile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
54+
compile 'io.grpc:grpc-netty-shaded:1.31.1'
55+
compile 'io.grpc:grpc-protobuf:1.31.1'
56+
compile 'io.grpc:grpc-stub:1.31.1'
5657
}
5758

5859
dependencyCheck {

fabric-chaincode-shim/src/main/java/commons-logging.properties

Lines changed: 0 additions & 32 deletions
This file was deleted.

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logging.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private static Level mapLevel(final String level) {
9393
case "CRITICAL":
9494
return Level.SEVERE;
9595
case "WARNING":
96+
case "WARN":
9697
return Level.WARNING;
9798
case "INFO":
9899
return Level.INFO;

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/metrics/impl/DefaultProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public void run() {
5353
protected void logMetrics() {
5454

5555
perflogger.info(() -> {
56+
if (DefaultProvider.this.taskService == null) {
57+
return "No Metrics Provider service yet";
58+
}
5659
final StringBuilder sb = new StringBuilder();
5760
sb.append('{');
5861
sb.append(String.format(" \"active_count\":%d ", DefaultProvider.this.taskService.getActiveCount())).append(',');

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

Lines changed: 23 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,19 @@
1313
import java.io.File;
1414
import java.io.IOException;
1515
import java.io.InputStream;
16-
import java.io.PrintWriter;
17-
import java.io.StringWriter;
1816
import java.nio.file.Files;
1917
import java.nio.file.Paths;
2018
import java.security.Security;
2119
import java.util.Base64;
22-
import java.util.Collections;
23-
import java.util.Date;
24-
import java.util.List;
2520
import java.util.Properties;
26-
import java.util.logging.Formatter;
2721
import java.util.logging.Level;
28-
import java.util.logging.LogManager;
2922
import java.util.logging.LogRecord;
3023
import java.util.logging.Logger;
3124
import java.util.logging.SimpleFormatter;
3225

26+
import com.google.protobuf.InvalidProtocolBufferException;
27+
import com.google.protobuf.util.JsonFormat;
28+
3329
import org.apache.commons.cli.CommandLine;
3430
import org.apache.commons.cli.DefaultParser;
3531
import org.apache.commons.cli.Options;
@@ -42,14 +38,11 @@
4238
import org.hyperledger.fabric.shim.impl.ChaincodeSupportClient;
4339
import org.hyperledger.fabric.shim.impl.InvocationTaskManager;
4440

45-
import com.google.protobuf.InvalidProtocolBufferException;
46-
import com.google.protobuf.util.JsonFormat;
47-
4841
import io.grpc.ManagedChannelBuilder;
49-
import io.grpc.netty.GrpcSslContexts;
50-
import io.grpc.netty.NegotiationType;
51-
import io.grpc.netty.NettyChannelBuilder;
52-
import io.netty.handler.ssl.SslContext;
42+
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts;
43+
import io.grpc.netty.shaded.io.grpc.netty.NegotiationType;
44+
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder;
45+
import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext;
5346

5447
public abstract class ChaincodeBase implements Chaincode {
5548

@@ -147,6 +140,7 @@ protected final void connectToPeer() throws IOException {
147140
}
148141

149142
protected final void initializeLogging() {
143+
// the VM wide formatting string.
150144
System.setProperty("java.util.logging.SimpleFormatter.format",
151145
"%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$-80.80s %5$s%6$s%n");
152146
final Logger rootLogger = Logger.getLogger("");
@@ -157,53 +151,12 @@ protected final void initializeLogging() {
157151

158152
@Override
159153
public synchronized String format(final LogRecord record) {
160-
return super.format(record).replaceFirst(".*SEVERE\\s*\\S*\\s*\\S*", "\u001B[1;31m$0\u001B[0m")
161-
.replaceFirst(".*WARNING\\s*\\S*\\s*\\S*", "\u001B[1;33m$0\u001B[0m")
162-
.replaceFirst(".*CONFIG\\s*\\S*\\s*\\S*", "\u001B[35m$0\u001B[0m")
163-
.replaceFirst(".*FINE\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m")
164-
.replaceFirst(".*FINER\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m")
165-
.replaceFirst(".*FINEST\\s*\\S*\\s*\\S*", "\u001B[36m$0\u001B[0m");
154+
return Thread.currentThread() + " " + super.format(record);
166155
}
167156

168157
});
169158
}
170159

171-
final LogManager logManager = LogManager.getLogManager();
172-
173-
final Formatter f = new Formatter() {
174-
175-
private final Date dat = new Date();
176-
private final String format = "%1$tH:%1$tM:%1$tS:%1$tL %4$-7.7s %2$-80.80s %5$s%6$s%n";
177-
178-
@Override
179-
public String format(final LogRecord record) {
180-
dat.setTime(record.getMillis());
181-
String source;
182-
if (record.getSourceClassName() != null) {
183-
source = record.getSourceClassName();
184-
if (record.getSourceMethodName() != null) {
185-
source += " " + record.getSourceMethodName();
186-
}
187-
} else {
188-
source = record.getLoggerName();
189-
}
190-
final String message = formatMessage(record);
191-
String throwable = "";
192-
if (record.getThrown() != null) {
193-
final StringWriter sw = new StringWriter();
194-
final PrintWriter pw = new PrintWriter(sw);
195-
pw.println();
196-
record.getThrown().printStackTrace(pw);
197-
pw.close();
198-
throwable = sw.toString();
199-
}
200-
return String.format(format, dat, source, record.getLoggerName(), record.getLevel(), message,
201-
throwable);
202-
203-
}
204-
205-
};
206-
207160
rootLogger.info("Updated all handlers the format");
208161
// set logging level of chaincode logger
209162
final Level chaincodeLogLevel = mapLevel(System.getenv(CORE_CHAINCODE_LOGGING_LEVEL));
@@ -223,31 +176,26 @@ public String format(final LogRecord record) {
223176
Logger.getLogger(ChaincodeBase.class.getPackage().getName()).setLevel(shimLogLevel);
224177
Logger.getLogger(ContractRouter.class.getPackage().getName()).setLevel(chaincodeLogLevel);
225178

226-
final List<?> loggers = Collections.list(LogManager.getLogManager().getLoggerNames());
227-
loggers.forEach(x -> {
228-
final Logger l = LogManager.getLogManager().getLogger((String) x);
229-
// err what is the code supposed to do?
230-
});
231-
232179
}
233180

234181
private Level mapLevel(final String level) {
235182

236183
if (level != null) {
237184
switch (level.toUpperCase().trim()) {
238-
case "CRITICAL":
239-
case "ERROR":
240-
return Level.SEVERE;
241-
case "WARNING":
242-
return Level.WARNING;
243-
case "INFO":
244-
return Level.INFO;
245-
case "NOTICE":
246-
return Level.CONFIG;
247-
case "DEBUG":
248-
return Level.FINEST;
249-
default:
250-
break;
185+
case "CRITICAL":
186+
case "ERROR":
187+
return Level.SEVERE;
188+
case "WARNING":
189+
case "WARN":
190+
return Level.WARNING;
191+
case "INFO":
192+
return Level.INFO;
193+
case "NOTICE":
194+
return Level.CONFIG;
195+
case "DEBUG":
196+
return Level.FINEST;
197+
default:
198+
break;
251199
}
252200
}
253201
return Level.INFO;

0 commit comments

Comments
 (0)