8000 Rdb index background (preliminary) by graetzer · Pull Request #7644 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

Rdb index background (preliminary) #7644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 39 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3f6967c
Initial commit
graetzer Dec 3, 2018
38b3500
make sure index is hidden
graetzer Dec 3, 2018
0ddb321
Merge branch 'devel' of github.com:arangodb/arangodb into feature/rdb…
graetzer Dec 4, 2018
9bcc737
last changes
graetzer Dec 4, 2018
c57de4f
fix a bug
graetzer Dec 4, 2018
c798357
reduce conflicts
graetzer Dec 5, 2018
4766900
fix background indexing
graetzer Dec 6, 2018
b525712
remove unused code
graetzer Dec 6, 2018
0515650
fix link creation
graetzer Dec 6, 2018
70ef2f1
fix unique constraint violations
graetzer Dec 6, 2018
e6d23c1
fixed arangosearch cluster reporting
graetzer Dec 6, 2018
13bcd44
added test
graetzer Dec 6, 2018
9e5f960
fix test
graetzer Dec 6, 2018
6bfd840
make noncluster for now
graetzer Dec 6, 2018
3d20521
fix jslint
graetzer Dec 7, 2018
38005e0
Some test adjustments.
Dec 10, 2018
dc041fb
Merge branch 'devel' into feature/rdb-index-background
Dec 10, 2018
a7ae28a
Fix merge error.
Dec 10, 2018
db46a40
changes
graetzer Dec 10, 2018
9db5709
Merge branch 'feature/rdb-index-background' of github.com:arangodb/ar…
graetzer Dec 10, 2018
ef4bb05
adding inBackground flag
graetzer Dec 11, 2018
5546745
Merge branch 'devel' into feature/rdb-index-background
Dec 11, 2018
2ebf591
Fi 8000 x merge errors.
Dec 11, 2018
2082f78
adding some docs
graetzer Dec 12, 2018
3efc632
Merge branch 'devel' into feature/rdb-index-background
Dec 12, 2018
98ddd16
Some small changes.
Dec 12, 2018
ee51fa5
Fixed removal bug and added test.
Dec 12, 2018
422e0ce
Added update test.
Dec 17, 2018
31d9d7b
Merge branch 'devel' of github.com:arangodb/arangodb into feature/rdb…
graetzer Dec 18, 2018
a34f928
forgot to comment out docs
graetzer Dec 18, 2018
021453c
fixing some code
graetzer Dec 19, 2018
f61b5e9
fix jslint
graetzer Dec 19, 2018
b013b15
remove some code
graetzer Dec 19, 2018
82f4b6e
fix reporting of unfinished indexes
graetzer Dec 20, 2018
b7d6a9d
Merge branch 'devel' of github.com:arangodb/arangodb into feature/rdb…
graetzer Dec 20, 2018
2f1cc80
fixing fillIndex for iresearch
graetzer Dec 21, 2018
371c738
revert a change
graetzer Dec 21, 2018
7f4e2e7
Merge branch 'devel' of github.com:arangodb/arangodb into feature/rdb…
graetzer Dec 21, 2018
3f46816
fixng a deadlock
graetzer Dec 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Documentation/Books/Manual/Indexing/Hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ details, including the index-identifier, is returned.
@endDocuBlock ensureHashIndexArray


<!-- Creating Hash Index in Background
---------------------------------

{% hint 'info' %}
This section only applies to the *rocksdb* storage engine
{% endhint %}

Creating new indexes is by default done under an exclusive collection lock. This means
that the collection (or the respective shards) are not available as long as the index
is created. This "foreground" index creation can be undesireable, if you have to perform it
on a live system without a dedicated maintenance window.

Indexes can also be created in "background", not using an exclusive lock during the creation.
The collection remains available, other CRUD operations can run on the collection while the index is created.
This can be achieved by using the *inBackground* option.

To create an hash index in the background in *arangosh* just specify `inBackground: true`:

```js
db.collection.ensureIndex({ type: "hash", fields: [ "value" ], inBackground: true });
``` -->

For more information see [Creating Indexes in Background](IndexBasics.md#creating-indexes-in-background)

Ensure uniqueness of relations in edge collections
--------------------------------------------------

Expand Down
124 changes: 99 additions & 25 deletions Documentation/Books/Manual/Indexing/IndexBasics.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ are covered by an edge collection's edge index automatically.
Using the system attribute `_id` in user-defined indexes is not possible, but
indexing `_key`, `_rev`, `_from`, and `_to` is.

<!-- Creating new indexes is usually done under an exclusive collection lock. The collection is not
available as long as the index is created. This "foreground" index creation can be undesireable,
if you have to perform it on a live system without a dedicated maintenance window.

For potentially long running index creation operations the _rocksdb_ storage-engine also supports
creating indexes in "background". The colletion remains available durint the index creation,
see the section [Creating Indexes in Background](#creating-indexes-in-background) for more information. -->

ArangoDB provides the following index types:

Primary Index
Expand Down Expand Up @@ -243,31 +251,6 @@ Skiplist indexes support [indexing array values](#indexing-array-values) if the
attribute name is extended with a <i>[\*]</i>`.


Persistent Index
----------------

The persistent index is a sorted index with persistence. The index entries are written to
disk when documents are stored or updated. That means the index entries do not need to be
rebuilt from the collection data when the server is restarted or the indexed collection
is initially loaded. Thus using persistent indexes may reduce collection loading times.

The persistent index type can be used for secondary indexes at the moment. That means the
persistent index currently cannot be made the only index for a collection, because there
will always be the in-memory primary index for the collection in addition, and potentially
more indexes (such as the edges index for an edge collection).

The index implementation is using the RocksDB engine, and it provides logarithmic complexity
for insert, update, and remove operations. As the persistent index is not an in-memory
index, it does not store pointers into the primary index as all the in-memory indexes do,
but instead it stores a document's primary key. To retrieve a document via a persistent
index via an index value lookup, there will therefore be an additional O(1) lookup into
the primary index to fetch the actual document.

As the persistent index is sorted, it can be used for point lookups, range queries and sorting
operations, but only if either all index attributes are provided in a query, or if a leftmost
prefix of the index attributes is specified.


Geo Index
---------

Expand Down Expand Up @@ -307,6 +290,37 @@ minimum length will be included in the index.
The fulltext index is used via dedicated functions in AQL or the simple queries, but will
not be enabled for other types of queries or conditions.


Persistent Index
----------------

{% hint 'warning' %}
this index should not be used anymore, instead use the rocksdb storage engine
with either the *skiplist* or *hash* index.
{% endhint %}

The persistent index is a sorted index with persistence. The index entries are written to
disk when documents are stored or updated. That means the index entries do not need to be
rebuilt from the collection data when the server is restarted or the indexed collection
is initially loaded. Thus using persistent indexes may reduce collection loading times.

The persistent index type can be used for secondary indexes at the moment. That means the
persistent index currently cannot be made the only index for a collection, because there
will always be the in-memory primary index for the collection in addition, and potentially
more indexes (such as the edges index for an edge collection).

The index implementation is using the RocksDB engine, and it provides logarithmic complexity
for insert, update, and remove operations. As the persistent index is not an in-memory
index, it does not store pointers into the primary index as all the in-memory indexes do,
but instead it stores a document's primary key. To retrieve a document via a persistent
index via an index value lookup, there will therefore be an additional O(1) lookup into
the primary index to fetch the actual document.

As the persistent index is sorted, it can be used for point lookups, range queries and sorting
operations, but only if either all index attributes are provided in a query, or if a leftmost
prefix of the index attributes is specified.


Indexing attributes and sub-attributes
--------------------------------------

Expand Down Expand Up @@ -534,3 +548,63 @@ optimizer may prefer the default edge index over vertex centric indexes
based on the costs it estimates, even if a vertex centric index might
in fact be faster. Vertex centric indexes are more likely to be chosen
for highly connected graphs and with RocksDB storage engine.

<!--
Creating Indexes in Background
------------------------------

{% hint 'info' %}
This section only applies to the *rocksdb* storage engine
{% endhint %}

Creating new indexes is by default done under an exclusive collection lock. This means
that the collection (or the respective shards) are not available as long as the index
is created. This "foreground" index creation can be undesireable, if you have to perform it
on a live system without a dedicated maintenance window.

Indexes can also be created in "background", not using an exclusive lock during the creation.
The collection remains available, other CRUD operations can run on the collection while the index is created.
This can be achieved by using the *inBackground* option.

To create a indexes in the background in *arangosh* just specify `inBackground: true`,
like in the following examples:

```js
// create the hash index in the background
db.collection.ensureIndex({ type: "hash", fields: [ "value" ], unique: false, inBackground: true });
db.collection.ensureIndex({ type: "hash", fields: [ "email" ], unique: true, inBackground: true });

// skiplist indexes work also of course
db.collection.ensureIndex({ type :"skiplist", fields: ["abc", "cdef"], unique: true, inBackground: true });
db.collection.ensureIndex({ type :"skiplist", fields: ["abc", "cdef"], sparse: true, inBackground: true });

// also supported on fulltext indexes
db.collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude"], inBackground: true });
db.collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude"], inBackground: true });
db.collection.ensureIndex({ type: "fulltext", fields: [ "text" ], minLength: 4, inBackground: true })
```

### Behaviour

Indexes that are still in the build process will not be visible via the ArangoDB API. Nevertheless it is not
possible to create the same index twice via the *ensureIndex* API. AQL Queries will not use these indexes either
until the indexes report back as finished. Note that the initial *ensureIndex* call or HTTP request will block until the index is completely ready. Existing single-threaded client programs can safely specify the
*inBackground* option as *true* and continue to work as before.

{% hint 'info' %}
Should you be building an index in the background you cannot rename or drop the collection.
These operations will block until the index creation is finished.
{% endhint %}

Interrupted index build (i.e. due to a server crash) will remove the partially build index.
In the ArangoDB cluster the index might then be automatically recreated on affected shards.

### Performance

The backround index creation might be slower than the "foreground" index creation and require more RAM.
Under a write heavy load (specifically many remove, update or replace) operations,
the background index creation needs to keep a list of removed documents in RAM. This might become unsuistainable
if this list grows to tens of millions of entries.

Building an index is always a write heavy operation (internally), it is alsways a good idea to build indexes
during times with less load. -->
25 changes: 25 additions & 0 deletions Documentation/Books/Manual/Indexing/Skiplist.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,28 @@ and
{ "a" : { "c" : 1, "b" : 1 } }
```
will match.


<!-- Creating Skiplist Index in Background
---------------------------------

{% hint 'info' %}
This section only applies to the *rocksdb* storage engine
{% endhint %}

Creating new indexes is by default done under an exclusive collection lock. This means
that the collection (or the respective shards) are not available as long as the index
is created. This "foreground" index creation can be undesireable, if you have to perform it
on a live system without a dedicated maintenance window.

Indexes can also be created in "background", not using an exclusive lock during the creation.
The collection remains available, other CRUD operations can run on the collection while the index is created.
This can be achieved by using the *inBackground* option.

To create a Skiplist index in the background in *arangosh* just specify `inBackground: true`:

```js
db.collection.ensureIndex({ type: "skiplist", fields: [ "value" ], inBackground: true });
```

For more information see [Creating Indexes in Background](IndexBasics.md#creating-indexes-in-background) -->
4 changes: 2 additions & 2 deletions arangod/Agency/Supervision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ void Supervision::readyOrphanedIndexCreations() {
indexes = collection("indexes").getArray();
if (indexes.length() > 0) {
for (auto const& planIndex : VPackArrayIterator(indexes)) {
if (planIndex.hasKey("isBuilding") && collection.has("shards")) {
if (planIndex.hasKey(StaticStrings::IndexIsBuilding) && collection.has("shards")) {
auto const& planId = planIndex.get("id");
auto const& shards = collection("shards");
if (collection.has("numberOfShards") &&
Expand Down Expand Up @@ -1121,7 +1121,7 @@ void Supervision::readyOrphanedIndexCreations() {
{ VPackObjectBuilder props(envelope.get());
for (auto const& prop : VPackObjectIterator(planIndex)) {
auto const& key = prop.key.copyString();
if (key != "isBuilding") {
if (key != StaticStrings::IndexIsBuilding) {
envelope->add(key, prop.value);
}
}}
Expand Down
16 changes: 8 additions & 8 deletions arangod/Aql/OptimizerRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6642,15 +6642,15 @@ static bool geoFuncArgCheck(ExecutionPlan* plan, AstNode const* args,
info.collectionNodeToReplace = collNode;
info.collectionNodeOutVar = collNode->outVariable();
info.collection = collNode->collection();
std::shared_ptr<LogicalCollection> coll =
collNode->collection()->getCollection();

// check for suitable indexes
for (std::shared_ptr<arangodb::Index> idx : coll->getIndexes()) {

// we should not access the LogicalCollection directly
Query* query = plan->getAst()->query();
auto indexes = query->trx()->indexesForCollection(info.collection->name());
// check for suitiable indexes
for (std::shared_ptr<arangodb::Index> idx : indexes) {
// check if current index is a geo-index
bool isGeo =
idx->type() == arangodb::Index::IndexType::TRI_IDX_TYPE_GEO_INDEX;
if (isGeo && idx->fields().size() == 1) { // individual fields
bool isGeo = idx->type() == arangodb::Index::IndexType::TRI_IDX_TYPE_GEO_INDEX;
if (isGeo && idx->fields().size() == 1) { // individual fields
// check access paths of attributes in ast and those in index match
if (idx->fields()[0] == attributeAccess.second) {
if (info.index != nullptr && info.index != idx) {
Expand Down
1 change: 0 additions & 1 deletion arangod/Aql/OptimizerRulesReplaceFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ std::pair<AstNode*, AstNode*> getAttributeAccessFromIndex(Ast* ast, AstNode* doc
for(auto& idx : indexes){
if(Index::isGeoIndex(idx->type())) {
// we take the first index that is found

bool isGeo1 = idx->type() == Index::IndexType::TRI_IDX_TYPE_GEO1_INDEX;
bool isGeo2 = idx->type() == Index::IndexType::TRI_IDX_TYPE_GEO2_INDEX;
bool isGeo = idx->type() == Index::IndexType::TRI_IDX_TYPE_GEO_INDEX;
Expand Down
13 changes: 5 additions & 8 deletions arangod/Cluster/ClusterInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2433,14 +2433,11 @@ int ClusterInfo::ensureIndexCoordinator(

// check index id
uint64_t iid = 0;

VPackSlice const idSlice = slice.get(StaticStrings::IndexId);
if (idSlice.isString()) {
// use predefined index id
if (idSlice.isString()) { // use predefined index id
iid = arangodb::basics::StringUtils::uint64(idSlice.copyString());
}
if (iid == 0) {
// no id set, create a new one!
if (iid == 0) { // no id set, create a new one!
iid = uniqid();
}
std::string const idString = arangodb::basics::StringUtils::itoa(iid);
Expand Down 9B53 Expand Up @@ -2629,14 +2626,14 @@ int ClusterInfo::ensureIndexCoordinatorInner(
for (auto const& e : VPackObjectIterator(slice)) {
TRI_ASSERT(e.key.isString());
std::string const& key = e.key.copyString();
if (key != StaticStrings::IndexId && key != "isBuilding") {
if (key != StaticStrings::IndexId && key != StaticStrings::IndexIsBuilding) {
ob->add(e.key);
ob->add(e.value);
}
}
if (numberOfShards > 0 &&
!slice.get(StaticStrings::IndexType).isEqualString("arangosearch")) {
ob->add("isBuilding", VPackValue(true));
ob->add(StaticStrings::IndexIsBuilding, VPackValue(true));
}
ob->add(StaticStrings::IndexId, VPackValue(idString));
}
Expand Down Expand Up @@ -2709,7 +2706,7 @@ int ClusterInfo::ensureIndexCoordinatorInner(
{ VPackObjectBuilder o(&finishedPlanIndex);
for (auto const& entry : VPackObjectIterator(newIndexBuilder.slice())) {
auto const key = entry.key.copyString();
if (key != "isBuilding" && key != "isNewlyCreated") {
if (key != StaticStrings::IndexIsBuilding && key != "isNewlyCreated") {
finishedPlanIndex.add(entry.key.copyString(), entry.value);
}
}
Expand Down
54 changes: 7 additions & 47 deletions arangod/ClusterEngine/ClusterCollection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,42 +360,6 @@ void ClusterCollection::prepareIndexes(
TRI_ASSERT(!_indexes.empty());
}

static std::shared_ptr<Index> findIndex(
velocypack::Slice const& info,
std::vector<std::shared_ptr<Index>> const& indexes) {
TRI_ASSERT(info.isObject());

// extract type
VPackSlice value = info.get("type");

if (!value.isString()) {
// Compatibility with old v8-vocindex.
THROW_ARANGO_EXCEPTION_MESSAGE(TRI_ERROR_INTERNAL,
"invalid index type definition");
}

std::string tmp = value.copyString();
arangodb::Index::IndexType const type = arangodb::Index::type(tmp.c_str());

for (auto const& idx : indexes) {
if (idx->type() == type) {
// Only check relevant indexes
if (idx->matchesDefinition(info)) {
// We found an index for this definition.
return idx;
}
}
}
return nullptr;
}

/// @brief Find index by definition
std::shared_ptr<Index> ClusterCollection::lookupIndex(
velocypack::Slice const& info) const {
READ_LOCKER(guard, _indexesLock);
return findIndex(info, _indexes);
}

std::shared_ptr<Index> ClusterCollection::createIndex(
arangodb::velocypack::Slice const& info, bool restore,
bool& created) {
Expand All @@ -404,23 +368,19 @@ std::shared_ptr<Index> ClusterCollection::createIndex(
WRITE_LOCKER(guard, _exclusiveLock);
std::shared_ptr<Index> idx;

{
WRITE_LOCKER(guard, _indexesLock);
idx = findIndex(info, _indexes);
if (idx) {
created = false;
// We already have this index.
return idx;
}
WRITE_LOCKER(guard2, _indexesLock);
idx = lookupIndex(info);
if (idx) {
created = false;
// We already have this index.
return idx;
}

StorageEngine* engine = EngineSelectorFeature::ENGINE;
TRI_ASSERT(engine != nullptr);

// We are sure that we do not have an index of this type.
// We also hold the lock.
// Create it

// We also hold the lock. Create it
idx = engine->indexFactory().prepareIndexFromSlice(
info, true, _logicalCollection, false
);
Expand Down
3 changes: 0 additions & 3 deletions arangod/ClusterEngine/ClusterCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ class ClusterCollection final : public PhysicalCollection {

void prepareIndexes(arangodb::velocypack::Slice indexesSlice) override;

/// @brief Find index by definition
std::shared_ptr<Index> lookupIndex(velocypack::Slice const&) const override;

std::shared_ptr<Index> createIndex(arangodb::velocypack::Slice const& info,
bool restore, bool& created) override;

Expand Down
Loading
0