Repository: incubator-blur Updated Branches: refs/heads/master e203a341d -> 7f24a6fb6
Fixing issue with user context not propogating across platform command calls. Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/7f24a6fb Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/7f24a6fb Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/7f24a6fb Branch: refs/heads/master Commit: 7f24a6fb62a6531485ae374eecee1865d8531f76 Parents: e203a34 Author: Aaron McCurry <[email protected]> Authored: Sun Feb 1 10:52:17 2015 -0500 Committer: Aaron McCurry <[email protected]> Committed: Sun Feb 1 10:52:17 2015 -0500 ---------------------------------------------------------------------- .../org/apache/blur/command/BlurObject.java | 25 +++++++++ .../org/apache/blur/command/CommandRunner.java | 6 +++ .../blur/command/ControllerClusterContext.java | 6 +++ .../apache/blur/command/UserCurrentUser.java | 53 ++++++++++++++++++++ .../apache/blur/thrift/BlurClusterTestBase.java | 28 +++++++++++ .../services/org.apache.blur.command.Commands | 1 + .../apache/blur/thrift/BlurClientManager.java | 19 ++++--- 7 files changed, 131 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/main/java/org/apache/blur/command/BlurObject.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/BlurObject.java b/blur-core/src/main/java/org/apache/blur/command/BlurObject.java index e61899d..841273a 100644 --- a/blur-core/src/main/java/org/apache/blur/command/BlurObject.java +++ b/blur-core/src/main/java/org/apache/blur/command/BlurObject.java @@ -352,4 +352,29 @@ public class BlurObject { return _valueMap.containsKey(key); } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((_valueMap == null) ? 0 : _valueMap.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + BlurObject other = (BlurObject) obj; + if (_valueMap == null) { + if (other._valueMap != null) + return false; + } else if (!_valueMap.equals(other._valueMap)) + return false; + return true; + } + } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java b/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java index 38c38aa..91df31a 100644 --- a/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java +++ b/blur-core/src/main/java/org/apache/blur/command/CommandRunner.java @@ -46,6 +46,7 @@ import org.apache.blur.thrift.generated.BlurException; import org.apache.blur.thrift.generated.ErrorType; import org.apache.blur.thrift.generated.Response; import org.apache.blur.thrift.generated.TimeoutException; +import org.apache.blur.trace.Tracer; public class CommandRunner { public static Connection[] getConnection(Iface client) { @@ -212,6 +213,7 @@ public class CommandRunner { Long executionId = null; Response response; INNER: while (true) { + Tracer tracer = BlurClientManager.setupClientPreCall(client); try { if (executionId == null) { response = client.execute(name, arguments); @@ -221,6 +223,10 @@ public class CommandRunner { break INNER; } catch (TimeoutException te) { executionId = te.getInstanceExecutionId(); + } finally { + if (tracer != null) { + tracer.done(); + } } } return CommandUtil.fromThriftResponseToObject(response); http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/main/java/org/apache/blur/command/ControllerClusterContext.java ---------------------------------------------------------------------- diff --git a/blur-core/src/main/java/org/apache/blur/command/ControllerClusterContext.java b/blur-core/src/main/java/org/apache/blur/command/ControllerClusterContext.java index 13b1589..0fb95f1 100644 --- a/blur-core/src/main/java/org/apache/blur/command/ControllerClusterContext.java +++ b/blur-core/src/main/java/org/apache/blur/command/ControllerClusterContext.java @@ -27,6 +27,7 @@ import org.apache.blur.thrift.generated.BlurException; import org.apache.blur.thrift.generated.Response; import org.apache.blur.thrift.generated.TimeoutException; import org.apache.blur.thrift.generated.ValueObject; +import org.apache.blur.trace.Tracer; /** * Licensed to the Apache Software Foundation (ASF) under one or more @@ -151,6 +152,7 @@ public class ControllerClusterContext extends ClusterContext implements Closeabl // the status of commands. Long executionId = null; while (true) { + Tracer tracer = BlurClientManager.setupClientPreCall(client); try { if (executionId == null) { return client.execute(command.getName(), arguments); @@ -164,6 +166,10 @@ public class ControllerClusterContext extends ClusterContext implements Closeabl LOG.info("Execution fetch timed out, reconnecting using [{0}].", executionId); } catch (TException e) { throw e; + } finally { + if (tracer != null) { + tracer.done(); + } } } } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/test/java/org/apache/blur/command/UserCurrentUser.java ---------------------------------------------------------------------- diff --git a/blur-core/src/test/java/org/apache/blur/command/UserCurrentUser.java b/blur-core/src/test/java/org/apache/blur/command/UserCurrentUser.java new file mode 100644 index 0000000..8cd9863 --- /dev/null +++ b/blur-core/src/test/java/org/apache/blur/command/UserCurrentUser.java @@ -0,0 +1,53 @@ +/** + * 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.blur.command; + +import java.io.IOException; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.blur.command.commandtype.IndexReadCommandSingleTable; +import org.apache.blur.user.User; +import org.apache.blur.user.UserContext; + +public class UserCurrentUser extends IndexReadCommandSingleTable<BlurObject> { + + @Override + public BlurObject execute(IndexContext context) throws IOException, InterruptedException { + BlurObject blurObject = new BlurObject(); + User user = UserContext.getUser(); + if (user == null) { + return blurObject; + } + blurObject.put("username", user.getUsername()); + Map<String, String> attributes = user.getAttributes(); + if (attributes != null) { + BlurObject blurObjectAttributes = new BlurObject(); + blurObject.put("attributes", blurObjectAttributes); + for (Entry<String, String> e : attributes.entrySet()) { + blurObjectAttributes.put(e.getKey(), e.getValue()); + } + } + return blurObject; + } + + @Override + public String getName() { + return "currentUser"; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/test/java/org/apache/blur/thrift/BlurClusterTestBase.java ---------------------------------------------------------------------- diff --git a/blur-core/src/test/java/org/apache/blur/thrift/BlurClusterTestBase.java b/blur-core/src/test/java/org/apache/blur/thrift/BlurClusterTestBase.java index 61b79ee..d68c760 100644 --- a/blur-core/src/test/java/org/apache/blur/thrift/BlurClusterTestBase.java +++ b/blur-core/src/test/java/org/apache/blur/thrift/BlurClusterTestBase.java @@ -33,6 +33,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Random; import java.util.Set; import java.util.TreeMap; @@ -45,7 +46,10 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.blur.MiniCluster; import org.apache.blur.TestType; import org.apache.blur.analysis.FieldManager; +import org.apache.blur.command.BlurObject; import org.apache.blur.command.RunSlowForTesting; +import org.apache.blur.command.Shard; +import org.apache.blur.command.UserCurrentUser; import org.apache.blur.server.TableContext; import org.apache.blur.thirdparty.thrift_0_9_0.TException; import org.apache.blur.thrift.generated.Blur; @@ -1001,4 +1005,28 @@ public abstract class BlurClusterTestBase { assertEquals(total, tableStats.getRecordCount()); } + @Test + public void testCommandUserProgation() throws IOException, BlurException, TException { + String table = "testCommandUserProgation"; + createTable(table); + Iface client = getClient(); + Map<String, String> attributes = new HashMap<String, String>(); + attributes.put("att1", "value"); + User user = new User(table, attributes); + UserContext.setUser(user); + UserCurrentUser userCurrentUser = new UserCurrentUser(); + userCurrentUser.setTable(table); + Map<Shard, BlurObject> currentUser = userCurrentUser.run(client); + + BlurObject blurObject = new BlurObject(); + blurObject.put("username", table); + BlurObject attributesBlurObject = new BlurObject(); + attributesBlurObject.put("att1", "value"); + blurObject.put("attributes", attributesBlurObject); + + for (Entry<Shard, BlurObject> e : currentUser.entrySet()) { + assertEquals(blurObject, e.getValue()); + } + } + } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-core/src/test/resources/META-INF/services/org.apache.blur.command.Commands ---------------------------------------------------------------------- diff --git a/blur-core/src/test/resources/META-INF/services/org.apache.blur.command.Commands b/blur-core/src/test/resources/META-INF/services/org.apache.blur.command.Commands index ce500da..710bab0 100644 --- a/blur-core/src/test/resources/META-INF/services/org.apache.blur.command.Commands +++ b/blur-core/src/test/resources/META-INF/services/org.apache.blur.command.Commands @@ -16,3 +16,4 @@ org.apache.blur.command.WaitForSeconds org.apache.blur.command.ThrowException org.apache.blur.command.RunSlowForTesting +org.apache.blur.command.UserCurrentUser http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/7f24a6fb/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java ---------------------------------------------------------------------- diff --git a/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java b/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java index 871caf6..8c0e9ba 100644 --- a/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java +++ b/blur-thrift/src/main/java/org/apache/blur/thrift/BlurClientManager.java @@ -187,13 +187,7 @@ public class BlurClientManager { } Tracer trace = null; try { - User user = UserConverter.toThriftUser(UserContext.getUser()); - client.get().setUser(user); - TraceId traceId = Trace.getTraceId(); - if (traceId != null) { - client.get().startTrace(traceId.getRootId(), traceId.getRequestId()); - trace = Trace.trace("thrift client", Trace.param("connection", getConnectionStr(client.get()))); - } + trace = setupClientPreCall(client.get()); T result = command.call((CLIENT) client.get(), connection); allBad = false; if (command.isDetachClient()) { @@ -249,6 +243,17 @@ public class BlurClientManager { } } + public static Tracer setupClientPreCall(Client client) throws TException { + User user = UserConverter.toThriftUser(UserContext.getUser()); + client.setUser(user); + TraceId traceId = Trace.getTraceId(); + if (traceId != null) { + client.startTrace(traceId.getRootId(), traceId.getRequestId()); + return Trace.trace("thrift client", Trace.param("connection", getConnectionStr(client))); + } + return null; + } + private static String getConnectionStr(Client client) { TTransport transport = client.getInputProtocol().getTransport(); if (transport instanceof TFramedTransport) {
