You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by GitBox <gi...@apache.org> on 2019/01/23 23:12:29 UTC

[GitHub] twdsilva commented on a change in pull request #428: PHOENIX-374: Enable access to dynamic columns in * or cf.* selection

twdsilva commented on a change in pull request #428: PHOENIX-374: Enable access to dynamic columns in * or cf.* selection
URL: https://github.com/apache/phoenix/pull/428#discussion_r250337420
 
 

 ##########
 File path: phoenix-core/src/main/java/org/apache/phoenix/coprocessor/DynamicColumnWildcard.java
 ##########
 @@ -0,0 +1,141 @@
+/*
+ * 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.phoenix.coprocessor;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hbase.Cell;
+import org.apache.hadoop.hbase.client.Mutation;
+import org.apache.hadoop.hbase.client.Put;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
+import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.RegionObserver;
+import org.apache.hadoop.hbase.regionserver.MiniBatchOperationInProgress;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.phoenix.coprocessor.generated.DynamicColumnMetaDataProtos;
+import org.apache.phoenix.coprocessor.generated.PTableProtos;
+import org.apache.phoenix.schema.PColumn;
+import org.apache.phoenix.schema.PColumnImpl;
+import org.apache.phoenix.util.ServerUtil;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NavigableMap;
+import java.util.Optional;
+
+/**
+ * Coprocessor for exposing dynamic columns in wildcard queries. This coprocessor will be registered
+ * to physical HBase tables only i.e. PTableType.TABLE and is driven by the client-side config
+ * QueryServices.WILDCARD_QUERY_DYNAMIC_COLS_ATTRIB
+ * See PHOENIX-374
+ */
+public class DynamicColumnWildcard implements RegionObserver, RegionCoprocessor {
+
+    private static final Log LOG = LogFactory.getLog(DynamicColumnWildcard.class);
+    // Scan attribute that is set in case we want to project dynamic columns
+    public static final String WILDCARD_SCAN_INCLUDES_DYNAMIC_COLUMNS =
+            "_WildcardScanIncludesDynCols";
+    public static final byte[] DYN_COLS_METADATA_CELL_QUALIFIER = Bytes.toBytes("#Dyn_");
+
+    @Override
+    public Optional<RegionObserver> getRegionObserver() {
+        return Optional.of(this);
+    }
+
+    @Override
+    public void preBatchMutate(ObserverContext<RegionCoprocessorEnvironment> c,
+            MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
+        try {
+            preBatchMutateWithExceptions(c, miniBatchOp);
+        } catch(Throwable t) {
+            // Wrap all exceptions in an IOException to prevent region server crashes
+            throw ServerUtil.createIOException("Unable to Put cells corresponding to dynamic" +
+                    "column metadata for " +
+                    c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString(), t);
+        }
+    }
+
+    /**
+     * Iterate over all Put mutations and add metadata for the list of dynamic columns for each
+     * column family in its own cell under reserved qualifiers. This is used for resolving dynamic
+     * columns for wildcard queries. See PHOENIX-374
+     * @param c the region server environment
+     * @param miniBatchOp batch of mutations getting applied to region
+     * @throws IOException If an I/O error occurs when parsing protobuf
+     */
+    private void preBatchMutateWithExceptions(ObserverContext<RegionCoprocessorEnvironment> c,
+            MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
+        for (int i = 0; i < miniBatchOp.size(); i++) {
+            Mutation m = miniBatchOp.getOperation(i);
+            List<Mutation> putDynColMetaData = new ArrayList<>(1);
+            boolean isThereAnExtraPut = false;
+            if (m instanceof Put) {
+                // There is at max 1 extra Put (for dynamic column shadow cells) per original Put
+                putDynColMetaData.add(new Put(m.getRow()));
+                NavigableMap<byte[], List<Cell>> famCellMap = m.getFamilyCellMap();
+                for (byte[] fam : famCellMap.keySet()) {
+                    byte[] serializedDynColsList = m.getAttribute(Bytes.toString(fam));
+                    if (serializedDynColsList == null) {
+                        // There are no dynamic columns for this column family
+                        continue;
+                    }
+                    List<PTableProtos.PColumn> dynColsInThisFam = DynamicColumnMetaDataProtos.
+                            DynamicColumnMetaData.parseFrom(serializedDynColsList)
+                            .getDynamicColumnsList();
+                    for (PTableProtos.PColumn dynColProto : dynColsInThisFam) {
+                        // Add a column for this dynamic column to the metadata Put operation
+                        ((Put)putDynColMetaData.get(0)).addColumn(fam,
 
 Review comment:
   nit: can you create a local variable for the put and then call ```miniBatchOp.addOperationsFromCP``` if its not null

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services