Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id B1D9F18399 for ; Wed, 27 May 2015 20:47:29 +0000 (UTC) Received: (qmail 25349 invoked by uid 500); 27 May 2015 20:47:29 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 25223 invoked by uid 500); 27 May 2015 20:47:29 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 25082 invoked by uid 99); 27 May 2015 20:47:29 -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; Wed, 27 May 2015 20:47:29 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 67306E07D6; Wed, 27 May 2015 20:47:29 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@accumulo.apache.org Date: Wed, 27 May 2015 20:47:32 -0000 Message-Id: <442e7683592b456989d043db00322a55@git.apache.org> In-Reply-To: <0746f4e8b05c48a4b91d8e401b6b83cf@git.apache.org> References: <0746f4e8b05c48a4b91d8e401b6b83cf@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [04/17] accumulo git commit: ACCUMULO-3856 Ensure batchwriter gets closed in updateAndFlush ACCUMULO-3856 Ensure batchwriter gets closed in updateAndFlush Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/11f108e2 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/11f108e2 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/11f108e2 Branch: refs/heads/master Commit: 11f108e252f8358a3ac8b79843f1ebd77bee647e Parents: 5db68da Author: Josh Elser Authored: Wed May 27 16:19:32 2015 -0400 Committer: Josh Elser Committed: Wed May 27 16:19:32 2015 -0400 ---------------------------------------------------------------------- proxy/pom.xml | 5 + .../org/apache/accumulo/proxy/ProxyServer.java | 16 ++- .../apache/accumulo/proxy/ProxyServerTest.java | 114 +++++++++++++++++++ 3 files changed, 131 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/11f108e2/proxy/pom.xml ---------------------------------------------------------------------- diff --git a/proxy/pom.xml b/proxy/pom.xml index d66a329..b75935a 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -94,6 +94,11 @@ zookeeper test + + org.easymock + easymock + test + http://git-wip-us.apache.org/repos/asf/accumulo/blob/11f108e2/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java ---------------------------------------------------------------------- diff --git a/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java b/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java index f873010..538fb03 100644 --- a/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java +++ b/proxy/src/main/java/org/apache/accumulo/proxy/ProxyServer.java @@ -1088,21 +1088,29 @@ public class ProxyServer implements AccumuloProxy.Iface { public void updateAndFlush(ByteBuffer login, String tableName, Map> cells) throws org.apache.accumulo.proxy.thrift.AccumuloException, org.apache.accumulo.proxy.thrift.AccumuloSecurityException, org.apache.accumulo.proxy.thrift.TableNotFoundException, org.apache.accumulo.proxy.thrift.MutationsRejectedException, TException { + BatchWriterPlusException bwpe = null; try { - BatchWriterPlusException bwpe = getWriter(login, tableName, null); + bwpe = getWriter(login, tableName, null); addCellsToWriter(cells, bwpe); if (bwpe.exception != null) throw bwpe.exception; bwpe.writer.flush(); - bwpe.writer.close(); } catch (Exception e) { handleExceptionMRE(e); + } finally { + if (null != bwpe) { + try { + bwpe.writer.close(); + } catch (MutationsRejectedException e) { + handleExceptionMRE(e); + } + } } } private static final ColumnVisibility EMPTY_VIS = new ColumnVisibility(); - private void addCellsToWriter(Map> cells, BatchWriterPlusException bwpe) { + void addCellsToWriter(Map> cells, BatchWriterPlusException bwpe) { if (bwpe.exception != null) return; @@ -1217,7 +1225,7 @@ public class ProxyServer implements AccumuloProxy.Iface { return bwpe; } - private BatchWriterPlusException getWriter(ByteBuffer login, String tableName, WriterOptions opts) throws Exception { + BatchWriterPlusException getWriter(ByteBuffer login, String tableName, WriterOptions opts) throws Exception { BatchWriterConfig cfg = new BatchWriterConfig(); if (opts != null) { if (opts.maxMemory != 0) http://git-wip-us.apache.org/repos/asf/accumulo/blob/11f108e2/proxy/src/test/java/org/apache/accumulo/proxy/ProxyServerTest.java ---------------------------------------------------------------------- diff --git a/proxy/src/test/java/org/apache/accumulo/proxy/ProxyServerTest.java b/proxy/src/test/java/org/apache/accumulo/proxy/ProxyServerTest.java new file mode 100644 index 0000000..ed4f313 --- /dev/null +++ b/proxy/src/test/java/org/apache/accumulo/proxy/ProxyServerTest.java @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.accumulo.proxy; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.accumulo.core.client.BatchWriter; +import org.apache.accumulo.core.client.MutationsRejectedException; +import org.apache.accumulo.proxy.ProxyServer.BatchWriterPlusException; +import org.apache.accumulo.proxy.thrift.ColumnUpdate; +import org.apache.accumulo.proxy.thrift.WriterOptions; +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +/** + * + */ +public class ProxyServerTest { + + private static final Charset UTF_8 = Charset.forName("UTF-8"); + + @Test + public void updateAndFlushClosesWriterOnExceptionFromAddCells() throws Exception { + ProxyServer server = EasyMock.createMockBuilder(ProxyServer.class).addMockedMethod("getWriter", ByteBuffer.class, String.class, WriterOptions.class) + .addMockedMethod("addCellsToWriter", Map.class, BatchWriterPlusException.class).createMock(); + BatchWriter writer = EasyMock.createMock(BatchWriter.class); + BatchWriterPlusException bwpe = new BatchWriterPlusException(); + bwpe.writer = writer; + MutationsRejectedException mre = EasyMock.createMock(MutationsRejectedException.class); + + final ByteBuffer login = ByteBuffer.wrap("my_login".getBytes(UTF_8)); + final String tableName = "table1"; + final Map> cells = new HashMap>(); + + EasyMock.expect(server.getWriter(login, tableName, null)).andReturn(bwpe); + server.addCellsToWriter(cells, bwpe); + EasyMock.expectLastCall(); + + // Set the exception + bwpe.exception = mre; + + writer.close(); + EasyMock.expectLastCall(); + + EasyMock.replay(server, writer, mre); + + try { + server.updateAndFlush(login, tableName, cells); + Assert.fail("Expected updateAndFlush to throw an exception"); + } catch (org.apache.accumulo.proxy.thrift.MutationsRejectedException e) { + // pass + } + + EasyMock.verify(server, writer, mre); + } + + @Test + public void updateAndFlushClosesWriterOnExceptionFromFlush() throws Exception { + ProxyServer server = EasyMock.createMockBuilder(ProxyServer.class).addMockedMethod("getWriter", ByteBuffer.class, String.class, WriterOptions.class) + .addMockedMethod("addCellsToWriter", Map.class, BatchWriterPlusException.class).createMock(); + BatchWriter writer = EasyMock.createMock(BatchWriter.class); + BatchWriterPlusException bwpe = new BatchWriterPlusException(); + bwpe.writer = writer; + MutationsRejectedException mre = EasyMock.createMock(MutationsRejectedException.class); + + final ByteBuffer login = ByteBuffer.wrap("my_login".getBytes(UTF_8)); + final String tableName = "table1"; + final Map> cells = new HashMap>(); + + EasyMock.expect(server.getWriter(login, tableName, null)).andReturn(bwpe); + server.addCellsToWriter(cells, bwpe); + EasyMock.expectLastCall(); + + // No exception throw adding the cells + bwpe.exception = null; + + writer.flush(); + EasyMock.expectLastCall().andThrow(mre); + + writer.close(); + EasyMock.expectLastCall(); + + EasyMock.replay(server, writer, mre); + + try { + server.updateAndFlush(login, tableName, cells); + Assert.fail("Expected updateAndFlush to throw an exception"); + } catch (org.apache.accumulo.proxy.thrift.MutationsRejectedException e) { + // pass + } + + EasyMock.verify(server, writer, mre); + } + +}