Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 9ABA4200BA6 for ; Mon, 3 Oct 2016 23:50:37 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 9964C160AE5; Mon, 3 Oct 2016 21:50:37 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E5DAC160AEC for ; Mon, 3 Oct 2016 23:50:36 +0200 (CEST) Received: (qmail 17167 invoked by uid 500); 3 Oct 2016 21:50:36 -0000 Mailing-List: contact commits-help@kudu.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@kudu.apache.org Delivered-To: mailing list commits@kudu.apache.org Received: (qmail 17078 invoked by uid 99); 3 Oct 2016 21:50:36 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 03 Oct 2016 21:50:36 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id E5FFDE0105; Mon, 3 Oct 2016 21:50:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: danburkert@apache.org To: commits@kudu.apache.org Date: Mon, 03 Oct 2016 21:50:38 -0000 Message-Id: <35ac861e7a1b4e4d8383277765b2dcce@git.apache.org> In-Reply-To: <4120ddde18874f919cbeb58d40cb0731@git.apache.org> References: <4120ddde18874f919cbeb58d40cb0731@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [4/8] kudu git commit: [java client] make DateFormat safe to use archived-at: Mon, 03 Oct 2016 21:50:37 -0000 [java client] make DateFormat safe to use Todd's error-prone patch discovered this problem in RowResult where we use a static DateFormat from multiple threads. The recommended way to do this is to use thread locals. Change-Id: I6d18ba34db0a0782fd0b7401e9aea7ae59c6b6f6 Reviewed-on: http://gerrit.cloudera.org:8080/4429 Tested-by: Kudu Jenkins Reviewed-by: Adar Dembo (cherry picked from commit b0b273e8271752b6eb04ba163981aad1c792e413) Reviewed-on: http://gerrit.cloudera.org:8080/4603 Reviewed-by: Jean-Daniel Cryans Tested-by: Jean-Daniel Cryans Project: http://git-wip-us.apache.org/repos/asf/kudu/repo Commit: http://git-wip-us.apache.org/repos/asf/kudu/commit/653ddf3a Tree: http://git-wip-us.apache.org/repos/asf/kudu/tree/653ddf3a Diff: http://git-wip-us.apache.org/repos/asf/kudu/diff/653ddf3a Branch: refs/heads/branch-1.0.x Commit: 653ddf3aae9787f3e2817af0618cab8882b16857 Parents: 80629bb Author: Jean-Daniel Cryans Authored: Thu Sep 15 16:21:23 2016 -0700 Committer: Jean-Daniel Cryans Committed: Mon Oct 3 21:40:55 2016 +0000 ---------------------------------------------------------------------- .../java/org/apache/kudu/client/RowResult.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/kudu/blob/653ddf3a/java/kudu-client/src/main/java/org/apache/kudu/client/RowResult.java ---------------------------------------------------------------------- diff --git a/java/kudu-client/src/main/java/org/apache/kudu/client/RowResult.java b/java/kudu-client/src/main/java/org/apache/kudu/client/RowResult.java index 0ef3e03..64871d9 100644 --- a/java/kudu-client/src/main/java/org/apache/kudu/client/RowResult.java +++ b/java/kudu-client/src/main/java/org/apache/kudu/client/RowResult.java @@ -39,10 +39,17 @@ import java.util.TimeZone; public class RowResult { private static final int INDEX_RESET_LOCATION = -1; - private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); - static { - DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); - } + + // Thread local DateFormat since they're not thread-safe. + private static final ThreadLocal DATE_FORMAT = new ThreadLocal(){ + @Override + protected DateFormat initialValue() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + return sdf; + } + }; + private static final long MS_IN_S = 1000L; private static final long US_IN_S = 1000L * 1000L; private int index = INDEX_RESET_LOCATION; @@ -513,7 +520,7 @@ public class RowResult { long tsMillis = timestamp / MS_IN_S; long tsMicros = timestamp % US_IN_S; StringBuffer formattedTs = new StringBuffer(); - DATE_FORMAT.format(new Date(tsMillis), formattedTs, new FieldPosition(0)); + DATE_FORMAT.get().format(new Date(tsMillis), formattedTs, new FieldPosition(0)); formattedTs.append(String.format(".%06dZ", tsMicros)); return formattedTs.toString(); }