[GitHub] [drill] jnturton commented on a diff in pull request #2722: DRILL-8371: Add Write/Insert Capability to Splunk Plugin

2022-12-22 Thread GitBox


jnturton commented on code in PR #2722:
URL: https://github.com/apache/drill/pull/2722#discussion_r1055192926


##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkBatchWriter.java:
##
@@ -98,27 +100,69 @@ public void updateSchema(VectorAccessible batch) {
   @Override
   public void startRecord() {
 logger.debug("Starting record");
-// Ensure that the new record is empty. This is not strictly necessary, 
but it is a belt and suspenders approach.
-splunkEvent.clear();
+// Ensure that the new record is empty.
+splunkEvent = new JSONObject();
   }
 
   @Override
-  public void endRecord() throws IOException {
+  public void endRecord() {
 logger.debug("Ending record");
+recordCount++;
+
+// Put event in buffer
+eventBuffer.add(splunkEvent);
+
 // Write the event to the Splunk index
-destinationIndex.submit(eventArgs, splunkEvent.toJSONString());
-// Clear out the splunk event.
-splunkEvent.clear();
+if (recordCount >= config.getPluginConfig().getWriterBatchSize()) {
+  try {
+writeEvents();
+  } catch (IOException e) {
+throw  UserException.dataWriteError(e)
+.message("Error writing data to Splunk: " + e.getMessage())
+.build(logger);
+  }
+
+  // Reset record count
+  recordCount = 0;
+}
   }
 
+
+  /*
+  args – Optional arguments for this stream. Valid parameters are: "host", 
"host_regex", "source", and "sourcetype".
+   */
   @Override
   public void abort() {
+logger.debug("Aborting writing records to Splunk.");
 // No op
   }
 
   @Override
   public void cleanup() {
-// No op
+try {
+  writeEvents();
+} catch (IOException e) {
+  throw  UserException.dataWriteError(e)
+  .message("Error writing data to Splunk: " + e.getMessage())
+  .build(logger);
+}
+  }
+
+  private void writeEvents() throws IOException {
+// Open the socket and stream, set up a timestamp
+destinationIndex.attachWith(new ReceiverBehavior() {

Review Comment:
   This results in a dedicated TCP socket being opened and closed for every 
writer batch.



##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkBatchWriter.java:
##
@@ -98,27 +100,69 @@ public void updateSchema(VectorAccessible batch) {
   @Override
   public void startRecord() {
 logger.debug("Starting record");
-// Ensure that the new record is empty. This is not strictly necessary, 
but it is a belt and suspenders approach.
-splunkEvent.clear();
+// Ensure that the new record is empty.
+splunkEvent = new JSONObject();
   }
 
   @Override
-  public void endRecord() throws IOException {
+  public void endRecord() {
 logger.debug("Ending record");
+recordCount++;
+
+// Put event in buffer
+eventBuffer.add(splunkEvent);
+
 // Write the event to the Splunk index
-destinationIndex.submit(eventArgs, splunkEvent.toJSONString());
-// Clear out the splunk event.
-splunkEvent.clear();
+if (recordCount >= config.getPluginConfig().getWriterBatchSize()) {
+  try {
+writeEvents();
+  } catch (IOException e) {
+throw  UserException.dataWriteError(e)
+.message("Error writing data to Splunk: " + e.getMessage())
+.build(logger);
+  }
+
+  // Reset record count
+  recordCount = 0;
+}
   }
 
+
+  /*
+  args – Optional arguments for this stream. Valid parameters are: "host", 
"host_regex", "source", and "sourcetype".
+   */
   @Override
   public void abort() {
+logger.debug("Aborting writing records to Splunk.");

Review Comment:
   Would there be any use in clearing eventBuffer here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@drill.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



[GitHub] [drill] jnturton commented on a diff in pull request #2722: DRILL-8371: Add Write/Insert Capability to Splunk Plugin

2022-12-19 Thread GitBox


jnturton commented on code in PR #2722:
URL: https://github.com/apache/drill/pull/2722#discussion_r1052404918


##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkBatchWriter.java:
##
@@ -0,0 +1,308 @@
+/*
+ * 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.drill.exec.store.splunk;
+
+
+import com.splunk.Args;
+import com.splunk.Index;
+import com.splunk.IndexCollection;
+import com.splunk.Service;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.proto.UserBitShared.UserCredentials;
+import org.apache.drill.exec.record.VectorAccessible;
+import org.apache.drill.exec.store.AbstractRecordWriter;
+import org.apache.drill.exec.store.EventBasedRecordWriter.FieldConverter;
+import org.apache.drill.exec.vector.complex.reader.FieldReader;
+import org.json.simple.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+public class SplunkBatchWriter extends AbstractRecordWriter {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(SplunkBatchWriter.class);
+  private static final String DEFAULT_SOURCETYPE = "drill";
+  private final UserCredentials userCredentials;
+  private final List tableIdentifier;
+  private final SplunkWriter config;
+  private final Args eventArgs;
+  protected final Service splunkService;
+  private JSONObject splunkEvent;
+  protected Index destinationIndex;
+
+
+  public SplunkBatchWriter(UserCredentials userCredentials, List 
tableIdentifier, SplunkWriter config) {
+this.config = config;
+this.tableIdentifier = tableIdentifier;
+this.userCredentials = userCredentials;
+
+SplunkConnection connection = new 
SplunkConnection(config.getPluginConfig(), userCredentials.getUserName());
+this.splunkService = connection.connect();
+
+// Populate event arguments
+this.eventArgs = new Args();
+eventArgs.put("sourcetype", DEFAULT_SOURCETYPE);
+  }
+
+  @Override
+  public void init(Map writerOptions) throws IOException {
+// No op
+  }
+
+  /**
+   * Update the schema in RecordWriter. Called at least once before starting 
writing the records. In this case,
+   * we add the index to Splunk here. Splunk's API is a little sparse and 
doesn't really do much in the way
+   * of error checking or providing feedback if the operation fails.
+   *
+   * @param batch {@link VectorAccessible} The incoming batch
+   */
+  @Override
+  public void updateSchema(VectorAccessible batch) {
+logger.debug("Updating schema for Splunk");
+
+//Get the collection of indexes
+IndexCollection indexes = splunkService.getIndexes();
+try {
+  String indexName = tableIdentifier.get(0);
+  indexes.create(indexName);
+  destinationIndex = splunkService.getIndexes().get(indexName);
+} catch (Exception e) {
+  // We have to catch a generic exception here, as Splunk's SDK does not 
really provide any kind of
+  // failure messaging.
+  throw UserException.systemError(e)
+.message("Error creating new index in Splunk plugin: " + 
e.getMessage())
+.build(logger);
+}
+  }
+
+
+  @Override
+  public void startRecord() {
+logger.debug("Starting record");
+splunkEvent = new JSONObject();
+  }
+
+  @Override
+  public void endRecord() throws IOException {
+logger.debug("Ending record");
+// Write the event to the Splunk index
+destinationIndex.submit(eventArgs, splunkEvent.toJSONString());

Review Comment:
   @cgivre can we leave a comment explaining this to readers then?



##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkBatchWriter.java:
##
@@ -0,0 +1,309 @@
+/*
+ * 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
+ *
+ * 

[GitHub] [drill] jnturton commented on a diff in pull request #2722: DRILL-8371: Add Write/Insert Capability to Splunk Plugin

2022-12-19 Thread GitBox


jnturton commented on code in PR #2722:
URL: https://github.com/apache/drill/pull/2722#discussion_r1052337380


##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkBatchWriter.java:
##
@@ -0,0 +1,308 @@
+/*
+ * 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.drill.exec.store.splunk;
+
+
+import com.splunk.Args;
+import com.splunk.Index;
+import com.splunk.IndexCollection;
+import com.splunk.Service;
+import org.apache.drill.common.exceptions.UserException;
+import org.apache.drill.exec.proto.UserBitShared.UserCredentials;
+import org.apache.drill.exec.record.VectorAccessible;
+import org.apache.drill.exec.store.AbstractRecordWriter;
+import org.apache.drill.exec.store.EventBasedRecordWriter.FieldConverter;
+import org.apache.drill.exec.vector.complex.reader.FieldReader;
+import org.json.simple.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+public class SplunkBatchWriter extends AbstractRecordWriter {
+
+  private static final Logger logger = 
LoggerFactory.getLogger(SplunkBatchWriter.class);
+  private static final String DEFAULT_SOURCETYPE = "drill";
+  private final UserCredentials userCredentials;
+  private final List tableIdentifier;
+  private final SplunkWriter config;
+  private final Args eventArgs;
+  protected final Service splunkService;
+  private JSONObject splunkEvent;
+  protected Index destinationIndex;
+
+
+  public SplunkBatchWriter(UserCredentials userCredentials, List 
tableIdentifier, SplunkWriter config) {
+this.config = config;
+this.tableIdentifier = tableIdentifier;
+this.userCredentials = userCredentials;
+
+SplunkConnection connection = new 
SplunkConnection(config.getPluginConfig(), userCredentials.getUserName());
+this.splunkService = connection.connect();
+
+// Populate event arguments
+this.eventArgs = new Args();
+eventArgs.put("sourcetype", DEFAULT_SOURCETYPE);
+  }
+
+  @Override
+  public void init(Map writerOptions) throws IOException {
+// No op
+  }
+
+  /**
+   * Update the schema in RecordWriter. Called at least once before starting 
writing the records. In this case,
+   * we add the index to Splunk here. Splunk's API is a little sparse and 
doesn't really do much in the way
+   * of error checking or providing feedback if the operation fails.
+   *
+   * @param batch {@link VectorAccessible} The incoming batch
+   */
+  @Override
+  public void updateSchema(VectorAccessible batch) {
+logger.debug("Updating schema for Splunk");
+
+//Get the collection of indexes
+IndexCollection indexes = splunkService.getIndexes();
+try {
+  String indexName = tableIdentifier.get(0);
+  indexes.create(indexName);
+  destinationIndex = splunkService.getIndexes().get(indexName);
+} catch (Exception e) {
+  // We have to catch a generic exception here, as Splunk's SDK does not 
really provide any kind of
+  // failure messaging.
+  throw UserException.systemError(e)
+.message("Error creating new index in Splunk plugin: " + 
e.getMessage())
+.build(logger);
+}
+  }
+
+
+  @Override
+  public void startRecord() {
+logger.debug("Starting record");
+splunkEvent = new JSONObject();
+  }
+
+  @Override
+  public void endRecord() throws IOException {
+logger.debug("Ending record");
+// Write the event to the Splunk index
+destinationIndex.submit(eventArgs, splunkEvent.toJSONString());
+// Clear out the splunk event.
+splunkEvent = new JSONObject();
+  }
+
+  @Override
+  public void abort() {
+// No op
+  }
+
+  @Override
+  public void cleanup() {
+// No op
+  }
+
+
+  @Override
+  public FieldConverter getNewNullableIntConverter(int fieldId, String 
fieldName, FieldReader reader) {
+return new ScalarSplunkConverter(fieldId, fieldName, reader);
+  }
+
+  @Override
+  public FieldConverter getNewIntConverter(int fieldId, String fieldName, 
FieldReader reader) {
+return new ScalarSplunkConverter(fieldId, fieldName, reader);
+  }
+
+  @Override
+  public FieldConverter getNewNullableBigIntConverter(int fieldId, String 
fieldName, 

[GitHub] [drill] jnturton commented on a diff in pull request #2722: DRILL-8371: Add Write/Insert Capability to Splunk Plugin

2022-12-19 Thread GitBox


jnturton commented on code in PR #2722:
URL: https://github.com/apache/drill/pull/2722#discussion_r1052328977


##
contrib/storage-splunk/src/main/java/org/apache/drill/exec/store/splunk/SplunkInsertWriter.java:
##
@@ -0,0 +1,72 @@
+/*
+ * 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.drill.exec.store.splunk;
+
+import com.fasterxml.jackson.annotation.JacksonInject;
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.store.StoragePluginRegistry;
+
+import java.util.List;
+
+public class SplunkInsertWriter extends SplunkWriter {
+  public static final String OPERATOR_TYPE = "SPLUNK_INSERT_WRITER";
+
+  private final SplunkStoragePlugin plugin;
+  private final List tableIdentifier;
+
+  @JsonCreator
+  public SplunkInsertWriter(
+  @JsonProperty("child") PhysicalOperator child,
+  @JsonProperty("tableIdentifier") List tableIdentifier,
+  @JsonProperty("storage") SplunkPluginConfig storageConfig,
+  @JacksonInject StoragePluginRegistry engineRegistry) {
+super(child, tableIdentifier, engineRegistry.resolve(storageConfig, 
SplunkStoragePlugin.class));

Review Comment:
   Did you mean to name this engineRegistry rather than, say, pluginRegistry?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@drill.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org