You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2021/04/06 15:14:37 UTC

[GitHub] [incubator-doris] acelyc111 commented on a change in pull request #5452: [Audit][Stream Load] Support audit function for stream load

acelyc111 commented on a change in pull request #5452:
URL: https://github.com/apache/incubator-doris/pull/5452#discussion_r607922735



##########
File path: be/src/runtime/CMakeLists.txt
##########
@@ -91,6 +91,7 @@ set(RUNTIME_FILES
     message_body_sink.cpp
     stream_load/stream_load_context.cpp
     stream_load/stream_load_executor.cpp
+        stream_load/stream_load_recorder.cpp

Review comment:
       ```suggestion
       stream_load/stream_load_recorder.cpp
   ```

##########
File path: be/src/http/action/stream_load.cpp
##########
@@ -533,4 +539,17 @@ Status StreamLoadAction::_data_saved_path(HttpRequest* req, std::string* file_pa
     return Status::OK();
 }
 
+void StreamLoadAction::_sava_stream_load_record(StreamLoadContext* ctx, const std::string& str) {
+    auto stream_load_recorder = StorageEngine::instance()->get_stream_load_recorder();
+    if (stream_load_recorder != nullptr) {
+        std::string key = std::to_string(ctx->start_micros + ctx->load_cost_micros) + "_" + ctx->label;
+        auto st = stream_load_recorder->put(key, str);
+        if (st.ok()) {

Review comment:
       Also need to log when put into rocksdb failed

##########
File path: be/src/runtime/stream_load/stream_load_recorder.cpp
##########
@@ -0,0 +1,124 @@
+// 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.
+
+#include "runtime/stream_load/stream_load_recorder.h"
+
+#include "common/config.h"
+#include "common/status.h"
+#include "rocksdb/db.h"
+
+#include "rocksdb/options.h"
+#include "rocksdb/slice.h"
+#include "rocksdb/slice_transform.h"
+#include "rocksdb/utilities/db_ttl.h"
+#include "util/time.h"
+
+
+namespace doris {
+const std::string STREAM_LOAD_POSTFIX = "/stream_load";
+
+StreamLoadRecorder::StreamLoadRecorder(const std::string& root_path)
+        : _root_path(root_path),
+          _db(nullptr),
+          _last_compaction_time(UnixMillis()) {
+}
+
+StreamLoadRecorder::~StreamLoadRecorder() {
+    if (_db != nullptr) {
+        for (auto handle : _handles) {
+            _db->DestroyColumnFamilyHandle(handle);
+            handle = nullptr;
+        }
+        delete _db;
+        _db= nullptr;
+    }
+}
+
+Status StreamLoadRecorder::init() {
+    // init db
+    rocksdb::DBOptions options;
+    options.IncreaseParallelism();
+    options.create_if_missing = true;
+    options.create_missing_column_families = true;
+    std::string db_path = _root_path + STREAM_LOAD_POSTFIX;
+    std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
+    // default column family is required
+    column_families.emplace_back(DEFAULT_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    // stream load column family
+    column_families.emplace_back(STREAM_LOAD_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    std::vector<int32_t> ttls = {config::stream_load_record_expire_time_secs, config::stream_load_record_expire_time_secs};
+    rocksdb::Status s = rocksdb::DBWithTTL::Open(options, db_path, column_families, &_handles, &_db, ttls);
+
+    if (!s.ok() || _db == nullptr) {
+        LOG(WARNING) << "rocks db open failed, reason:" << s.ToString();
+        return Status::InternalError("Stream load record rocksdb open failed");
+    }
+    return Status::OK();
+}
+
+Status StreamLoadRecorder::put(const std::string& key, const std::string& value) {
+    rocksdb::ColumnFamilyHandle* handle = _handles[1];
+    rocksdb::WriteOptions write_options;
+    write_options.sync = false;
+    rocksdb::Status s = _db->Put(write_options, handle, rocksdb::Slice(key), rocksdb::Slice(value));
+    if (!s.ok()) {
+        LOG(WARNING) << "rocks db put key:" << key << " failed, reason:" << s.ToString();
+        return Status::InternalError("Stream load record rocksdb put failed");

Review comment:
       Same as above.

##########
File path: be/src/runtime/stream_load/stream_load_recorder.cpp
##########
@@ -0,0 +1,124 @@
+// 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.
+
+#include "runtime/stream_load/stream_load_recorder.h"
+
+#include "common/config.h"
+#include "common/status.h"
+#include "rocksdb/db.h"
+
+#include "rocksdb/options.h"
+#include "rocksdb/slice.h"
+#include "rocksdb/slice_transform.h"
+#include "rocksdb/utilities/db_ttl.h"
+#include "util/time.h"
+
+
+namespace doris {
+const std::string STREAM_LOAD_POSTFIX = "/stream_load";
+
+StreamLoadRecorder::StreamLoadRecorder(const std::string& root_path)
+        : _root_path(root_path),
+          _db(nullptr),
+          _last_compaction_time(UnixMillis()) {
+}
+
+StreamLoadRecorder::~StreamLoadRecorder() {
+    if (_db != nullptr) {
+        for (auto handle : _handles) {
+            _db->DestroyColumnFamilyHandle(handle);
+            handle = nullptr;
+        }
+        delete _db;
+        _db= nullptr;
+    }
+}
+
+Status StreamLoadRecorder::init() {
+    // init db
+    rocksdb::DBOptions options;
+    options.IncreaseParallelism();
+    options.create_if_missing = true;
+    options.create_missing_column_families = true;
+    std::string db_path = _root_path + STREAM_LOAD_POSTFIX;
+    std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
+    // default column family is required
+    column_families.emplace_back(DEFAULT_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    // stream load column family
+    column_families.emplace_back(STREAM_LOAD_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    std::vector<int32_t> ttls = {config::stream_load_record_expire_time_secs, config::stream_load_record_expire_time_secs};
+    rocksdb::Status s = rocksdb::DBWithTTL::Open(options, db_path, column_families, &_handles, &_db, ttls);
+
+    if (!s.ok() || _db == nullptr) {
+        LOG(WARNING) << "rocks db open failed, reason:" << s.ToString();
+        return Status::InternalError("Stream load record rocksdb open failed");
+    }
+    return Status::OK();
+}
+
+Status StreamLoadRecorder::put(const std::string& key, const std::string& value) {
+    rocksdb::ColumnFamilyHandle* handle = _handles[1];
+    rocksdb::WriteOptions write_options;
+    write_options.sync = false;
+    rocksdb::Status s = _db->Put(write_options, handle, rocksdb::Slice(key), rocksdb::Slice(value));
+    if (!s.ok()) {
+        LOG(WARNING) << "rocks db put key:" << key << " failed, reason:" << s.ToString();
+        return Status::InternalError("Stream load record rocksdb put failed");
+    }
+
+    if ((UnixMillis() - _last_compaction_time) / 1000 > config::clean_stream_load_record_interval_secs) {
+        rocksdb::CompactRangeOptions options;
+        s = _db->CompactRange(options, _handles[1], nullptr, nullptr);

Review comment:
       The expired data will be GCed in rocksdb compaction procedure, not needed to compact it manually, and CompactRange may cost much resouce and time. Why you do that?

##########
File path: be/src/runtime/stream_load/stream_load_recorder.cpp
##########
@@ -0,0 +1,124 @@
+// 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.
+
+#include "runtime/stream_load/stream_load_recorder.h"
+
+#include "common/config.h"
+#include "common/status.h"
+#include "rocksdb/db.h"
+
+#include "rocksdb/options.h"
+#include "rocksdb/slice.h"
+#include "rocksdb/slice_transform.h"
+#include "rocksdb/utilities/db_ttl.h"
+#include "util/time.h"
+
+
+namespace doris {
+const std::string STREAM_LOAD_POSTFIX = "/stream_load";
+
+StreamLoadRecorder::StreamLoadRecorder(const std::string& root_path)
+        : _root_path(root_path),
+          _db(nullptr),
+          _last_compaction_time(UnixMillis()) {
+}
+
+StreamLoadRecorder::~StreamLoadRecorder() {
+    if (_db != nullptr) {
+        for (auto handle : _handles) {
+            _db->DestroyColumnFamilyHandle(handle);
+            handle = nullptr;
+        }
+        delete _db;
+        _db= nullptr;

Review comment:
       ```suggestion
           _db = nullptr;
   ```

##########
File path: be/src/runtime/stream_load/stream_load_recorder.h
##########
@@ -0,0 +1,57 @@
+// 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.
+
+#include <map>
+#include <string>
+
+#include "rocksdb/utilities/db_ttl.h"
+
+#pragma once
+
+namespace doris {
+
+class Status;
+
+class StreamLoadRecorder {
+public:
+    StreamLoadRecorder(const std::string& root_path);
+
+    virtual ~StreamLoadRecorder();
+
+    Status init();
+
+    Status put(const std::string& key, const std::string& value);
+
+    Status get_batch(const std::string& start, const int batch_size, std::map<std::string, std::string>* stream_load_records);
+
+private:
+    std::string _root_path;
+    rocksdb::DBWithTTL* _db;
+    std::vector<rocksdb::ColumnFamilyHandle*> _handles;
+
+    int64_t _last_compaction_time;
+
+    enum ColumnFamilyIndex {
+        DEFAULT_COLUMN_FAMILY_INDEX = 0,
+        STREAM_LOAD_COLUMN_FAMILY_INDEX
+    };
+
+    const std::string DEFAULT_COLUMN_FAMILY = "default";
+    const std::string STREAM_LOAD_COLUMN_FAMILY = "stream_load";

Review comment:
       Why you use multiple column families? you can use the default column family directly.

##########
File path: be/src/runtime/stream_load/stream_load_recorder.cpp
##########
@@ -0,0 +1,124 @@
+// 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.
+
+#include "runtime/stream_load/stream_load_recorder.h"
+
+#include "common/config.h"
+#include "common/status.h"
+#include "rocksdb/db.h"
+
+#include "rocksdb/options.h"
+#include "rocksdb/slice.h"
+#include "rocksdb/slice_transform.h"
+#include "rocksdb/utilities/db_ttl.h"
+#include "util/time.h"
+
+
+namespace doris {
+const std::string STREAM_LOAD_POSTFIX = "/stream_load";
+
+StreamLoadRecorder::StreamLoadRecorder(const std::string& root_path)
+        : _root_path(root_path),
+          _db(nullptr),
+          _last_compaction_time(UnixMillis()) {
+}
+
+StreamLoadRecorder::~StreamLoadRecorder() {
+    if (_db != nullptr) {
+        for (auto handle : _handles) {
+            _db->DestroyColumnFamilyHandle(handle);
+            handle = nullptr;
+        }
+        delete _db;
+        _db= nullptr;
+    }
+}
+
+Status StreamLoadRecorder::init() {
+    // init db
+    rocksdb::DBOptions options;
+    options.IncreaseParallelism();
+    options.create_if_missing = true;
+    options.create_missing_column_families = true;
+    std::string db_path = _root_path + STREAM_LOAD_POSTFIX;
+    std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
+    // default column family is required
+    column_families.emplace_back(DEFAULT_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    // stream load column family
+    column_families.emplace_back(STREAM_LOAD_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    std::vector<int32_t> ttls = {config::stream_load_record_expire_time_secs, config::stream_load_record_expire_time_secs};
+    rocksdb::Status s = rocksdb::DBWithTTL::Open(options, db_path, column_families, &_handles, &_db, ttls);
+
+    if (!s.ok() || _db == nullptr) {
+        LOG(WARNING) << "rocks db open failed, reason:" << s.ToString();
+        return Status::InternalError("Stream load record rocksdb open failed");

Review comment:
       You can append s.ToString() to the returned value, then the caller could know the error details, or he should grep the log to find the root cause.

##########
File path: be/src/runtime/stream_load/stream_load_recorder.cpp
##########
@@ -0,0 +1,124 @@
+// 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.
+
+#include "runtime/stream_load/stream_load_recorder.h"
+
+#include "common/config.h"
+#include "common/status.h"
+#include "rocksdb/db.h"
+
+#include "rocksdb/options.h"
+#include "rocksdb/slice.h"
+#include "rocksdb/slice_transform.h"
+#include "rocksdb/utilities/db_ttl.h"
+#include "util/time.h"
+
+
+namespace doris {
+const std::string STREAM_LOAD_POSTFIX = "/stream_load";
+
+StreamLoadRecorder::StreamLoadRecorder(const std::string& root_path)
+        : _root_path(root_path),
+          _db(nullptr),
+          _last_compaction_time(UnixMillis()) {
+}
+
+StreamLoadRecorder::~StreamLoadRecorder() {
+    if (_db != nullptr) {
+        for (auto handle : _handles) {
+            _db->DestroyColumnFamilyHandle(handle);
+            handle = nullptr;
+        }
+        delete _db;
+        _db= nullptr;
+    }
+}
+
+Status StreamLoadRecorder::init() {
+    // init db
+    rocksdb::DBOptions options;
+    options.IncreaseParallelism();
+    options.create_if_missing = true;
+    options.create_missing_column_families = true;
+    std::string db_path = _root_path + STREAM_LOAD_POSTFIX;
+    std::vector<rocksdb::ColumnFamilyDescriptor> column_families;
+    // default column family is required
+    column_families.emplace_back(DEFAULT_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    // stream load column family
+    column_families.emplace_back(STREAM_LOAD_COLUMN_FAMILY, rocksdb::ColumnFamilyOptions());
+    std::vector<int32_t> ttls = {config::stream_load_record_expire_time_secs, config::stream_load_record_expire_time_secs};
+    rocksdb::Status s = rocksdb::DBWithTTL::Open(options, db_path, column_families, &_handles, &_db, ttls);
+

Review comment:
       ```suggestion
   ```




-- 
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.

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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org