Skip to content

Commit

Permalink
#362: Cache index table metadata in tserver when updating an index
Browse files Browse the repository at this point in the history
Summary: Cache index table metadata in tserver when updating an index for performance. The cache is invalidated when the indexed table is altered.

Test Plan:
```
java -jar ~/code/yugabyte/java/yb-loadtester/target/yb-sample-apps.jar -workload CassandraSecondaryIndex -num_threads_read 0 -num_threads_write 4 -nodes 127.0.0.1:9042
```
Without cache:

```
2018-07-05 17:07:41,973 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 215.81 ops/sec (18.53 ms/op), 3517 total ops  |  Uptime: 25015 ms | maxWrittenKey: 3514 | maxGeneratedKey: 3520 |
2018-07-05 17:07:46,976 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 217.65 ops/sec (18.37 ms/op), 4606 total ops  |  Uptime: 30018 ms | maxWrittenKey: 4605 | maxGeneratedKey: 4609 |
2018-07-05 17:07:51,979 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 212.29 ops/sec (18.82 ms/op), 5668 total ops  |  Uptime: 35021 ms | maxWrittenKey: 5665 | maxGeneratedKey: 5671 |
```

With cache:

```
2018-07-05 17:12:24,773 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 229.69 ops/sec (17.42 ms/op), 2892 total ops  |  Uptime: 20015 ms | maxWrittenKey: 2891 | maxGeneratedKey: 2895 |
2018-07-05 17:12:29,776 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 231.24 ops/sec (17.31 ms/op), 4049 total ops  |  Uptime: 25018 ms | maxWrittenKey: 4048 | maxGeneratedKey: 4052 |
2018-07-05 17:12:34,781 [INFO|com.yugabyte.sample.common.metrics.MetricsTracker|MetricsTracker] Read: 0.00 ops/sec (0.00 ms/op), 0 total ops  |  Write: 235.37 ops/sec (16.97 ms/op), 5227 total ops  |  Uptime: 30023 ms | maxWrittenKey: 5225 | maxGeneratedKey: 5230 |
```

Also verify that no further RPC is executed at the master leader when the workload is run.

Reviewers: pritam.damania, amitanand, mihnea

Reviewed By: mihnea

Subscribers: ybase, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D5112
  • Loading branch information
robertpang committed Jul 6, 2018
1 parent 4dd62b7 commit 2ebadb8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
34 changes: 24 additions & 10 deletions src/yb/tablet/tablet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ Tablet::Tablet(
}
}

// Create index table metadata cache for secondary index update.
if (!metadata_->index_map().empty()) {
metadata_cache_.emplace(client_future_.get());
}

// TODO(dtxn) Create coordinator only for status tablets
if (transaction_coordinator_context) {
transaction_coordinator_ = std::make_unique<TransactionCoordinator>(
Expand Down Expand Up @@ -901,7 +906,12 @@ Status Tablet::UpdateQLIndexes(docdb::DocOperations* doc_ops) {
vector<std::pair<const IndexInfo*, shared_ptr<client::YBqlWriteOp>>> index_ops;
for (auto& pair : *write_op->index_requests()) {
client::YBTablePtr index_table;
RETURN_NOT_OK(client->OpenTable(pair.first->table_id(), &index_table));
bool cache_used_ignored = false;
if (!metadata_cache_) {
return STATUS(Corruption, "Table metadata cache is not present for index update");
}
RETURN_NOT_OK(metadata_cache_->GetTable(pair.first->table_id(), &index_table,
&cache_used_ignored));
shared_ptr<client::YBqlWriteOp> index_op(index_table->NewQLWrite());
index_op->mutable_request()->Swap(&pair.second);
index_op->mutable_request()->MergeFrom(pair.second);
Expand Down Expand Up @@ -1215,18 +1225,22 @@ Status Tablet::AlterSchema(AlterSchemaOperationState *operation_state) {
}
}

// Clear old index table metadata cache.
metadata_cache_ = boost::none;

// Update the index info.
metadata_->SetIndexMap(std::move(operation_state->index_map()));

// Create transaction manager for secondary index update.
if (!metadata_->index_map().empty() &&
metadata_->schema().table_properties().is_transactional() &&
!transaction_manager_) {
const auto transaction_participant_context =
DCHECK_NOTNULL(transaction_participant_.get())->context();
transaction_manager_.emplace(client_future_.get(),
transaction_participant_context->clock_ptr(),
local_tablet_filter_);
// Create transaction manager and index table metadata cache for secondary index update.
if (!metadata_->index_map().empty()) {
if (metadata_->schema().table_properties().is_transactional() && !transaction_manager_) {
const auto transaction_participant_context =
DCHECK_NOTNULL(transaction_participant_.get())->context();
transaction_manager_.emplace(client_future_.get(),
transaction_participant_context->clock_ptr(),
local_tablet_filter_);
}
metadata_cache_.emplace(client_future_.get());
}

// If the current schema and the new one are equal, there is nothing to do.
Expand Down
6 changes: 4 additions & 2 deletions src/yb/tablet/tablet.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

#include "yb/tserver/tserver.pb.h"

#include "yb/client/client_fwd.h"
#include "yb/client/client.h"
#include "yb/client/transaction_manager.h"

#include "yb/common/schema.h"
Expand Down Expand Up @@ -638,9 +638,11 @@ class Tablet : public AbstractTablet, public TransactionIntentApplier {

std::unique_ptr<TransactionParticipant> transaction_participant_;

// Created only when secondary indexes are present.
std::shared_future<client::YBClientPtr> client_future_;

// Created only when secondary indexes are present.
boost::optional<client::TransactionManager> transaction_manager_;
boost::optional<client::YBMetaDataCache> metadata_cache_;

std::atomic<int64_t> last_committed_write_index_{0};

Expand Down

0 comments on commit 2ebadb8

Please sign in to comment.