Skip to content

Commit 6508067

Browse files
gvvinbladedevozerov
authored andcommitted
IGNITE-6931: SQL: simplify index rebuild. Now both CREATE INDEX and "rebuild from hash" routines use the same iteration and entry locking logic. This closes apache#3052.
1 parent db343b6 commit 6508067

File tree

10 files changed

+166
-177
lines changed

10 files changed

+166
-177
lines changed

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheEntryEx.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.ignite.internal.processors.cache.version.GridCacheVersion;
3535
import org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx;
3636
import org.apache.ignite.internal.processors.dr.GridDrType;
37+
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheFilter;
3738
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure;
3839
import org.apache.ignite.internal.util.lang.GridTuple3;
3940
import org.jetbrains.annotations.Nullable;
@@ -869,12 +870,13 @@ public Collection<GridCacheMvccCandidate> localCandidates(@Nullable GridCacheVer
869870
/**
870871
* Update index from within entry lock, passing key, value, and expiration time to provided closure.
871872
*
873+
* @param filter Row filter.
872874
* @param clo Closure to apply to key, value, and expiration time.
873875
* @throws IgniteCheckedException If failed.
874876
* @throws GridCacheEntryRemovedException If entry was removed.
875877
*/
876-
public void updateIndex(SchemaIndexCacheVisitorClosure clo) throws IgniteCheckedException,
877-
GridCacheEntryRemovedException;
878+
public void updateIndex(SchemaIndexCacheFilter filter, SchemaIndexCacheVisitorClosure clo)
879+
throws IgniteCheckedException, GridCacheEntryRemovedException;
878880

879881
/**
880882
* @return Expire time, without accounting for transactions or removals.
@@ -918,13 +920,6 @@ public void updateIndex(SchemaIndexCacheVisitorClosure clo) throws IgniteChecked
918920
*/
919921
public void updateTtl(@Nullable GridCacheVersion ver, long ttl) throws GridCacheEntryRemovedException;
920922

921-
/**
922-
* Ensures that the value stored in the entry is also inserted in the indexing.
923-
*
924-
* @throws GridCacheEntryRemovedException If entry was removed.
925-
*/
926-
public void ensureIndexed() throws GridCacheEntryRemovedException, IgniteCheckedException;
927-
928923
/**
929924
* @return Value.
930925
* @throws IgniteCheckedException If failed to read from swap storage.

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheMapEntry.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.apache.ignite.internal.processors.cache.version.GridCacheVersionEx;
6161
import org.apache.ignite.internal.processors.cache.version.GridCacheVersionedEntryEx;
6262
import org.apache.ignite.internal.processors.dr.GridDrType;
63+
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheFilter;
6364
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorClosure;
6465
import org.apache.ignite.internal.util.IgniteTree;
6566
import org.apache.ignite.internal.util.lang.GridClosureException;
@@ -3140,16 +3141,6 @@ private IgniteTxLocalAdapter currentTx() {
31403141
}
31413142
}
31423143

3143-
/** {@inheritDoc} */
3144-
@Override public void ensureIndexed() throws GridCacheEntryRemovedException, IgniteCheckedException {
3145-
synchronized (this) {
3146-
checkObsolete();
3147-
3148-
if (cctx.queries().enabled())
3149-
cctx.offheap().updateIndexes(cctx, key, localPartition());
3150-
}
3151-
}
3152-
31533144
/** {@inheritDoc} */
31543145
@Override public synchronized CacheObject valueBytes() throws GridCacheEntryRemovedException {
31553146
checkObsolete();
@@ -3300,8 +3291,8 @@ protected void removeValue() throws IgniteCheckedException {
33003291
}
33013292

33023293
/** {@inheritDoc} */
3303-
@Override public void updateIndex(SchemaIndexCacheVisitorClosure clo) throws IgniteCheckedException,
3304-
GridCacheEntryRemovedException {
3294+
@Override public void updateIndex(SchemaIndexCacheFilter filter, SchemaIndexCacheVisitorClosure clo)
3295+
throws IgniteCheckedException, GridCacheEntryRemovedException {
33053296
synchronized (this) {
33063297
if (isInternal())
33073298
return;
@@ -3310,7 +3301,7 @@ protected void removeValue() throws IgniteCheckedException {
33103301

33113302
CacheDataRow row = cctx.offheap().read(this);
33123303

3313-
if (row != null)
3304+
if (row != null && (filter == null || filter.apply(row)))
33143305
clo.apply(row);
33153306
}
33163307
}

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManager.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,6 @@ public void update(
183183
@Nullable CacheDataRow oldRow
184184
) throws IgniteCheckedException;
185185

186-
/**
187-
* @param cctx Cache context.
188-
* @param key Key.
189-
* @param part Partition.
190-
* @throws IgniteCheckedException If failed.
191-
*/
192-
public void updateIndexes(
193-
GridCacheContext cctx,
194-
KeyCacheObject key,
195-
GridDhtLocalPartition part
196-
) throws IgniteCheckedException;
197-
198186
/**
199187
* @param cctx Cache context.
200188
* @param key Key.
@@ -451,13 +439,6 @@ void update(
451439
long expireTime,
452440
@Nullable CacheDataRow oldRow) throws IgniteCheckedException;
453441

454-
/**
455-
* @param cctx Cache context.
456-
* @param key Key.
457-
* @throws IgniteCheckedException If failed.
458-
*/
459-
void updateIndexes(GridCacheContext cctx, KeyCacheObject key) throws IgniteCheckedException;
460-
461442
/**
462443
* @param cctx Cache context.
463444
* @param key Key.

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/IgniteCacheOffheapManagerImpl.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,6 @@ private Iterator<CacheDataStore> cacheData(boolean primary, boolean backup, Affi
358358
dataStore(part).update(cctx, key, val, ver, expireTime, oldRow);
359359
}
360360

361-
/** {@inheritDoc} */
362-
@Override public void updateIndexes(GridCacheContext cctx, KeyCacheObject key, GridDhtLocalPartition part)
363-
throws IgniteCheckedException {
364-
dataStore(part).updateIndexes(cctx, key);
365-
}
366-
367361
/** {@inheritDoc} */
368362
@Override public void remove(
369363
GridCacheContext cctx,
@@ -1361,21 +1355,6 @@ private void finishUpdate(GridCacheContext cctx, CacheDataRow newRow, @Nullable
13611355
updateIgfsMetrics(cctx, key, (oldRow != null ? oldRow.value() : null), newRow.value());
13621356
}
13631357

1364-
/** {@inheritDoc} */
1365-
@Override public void updateIndexes(GridCacheContext cctx, KeyCacheObject key) throws IgniteCheckedException {
1366-
int cacheId = grp.sharedGroup() ? cctx.cacheId() : CU.UNDEFINED_CACHE_ID;
1367-
1368-
CacheDataRow row = dataTree.findOne(new SearchRow(cacheId, key), CacheDataRowAdapter.RowData.NO_KEY);
1369-
1370-
if (row != null) {
1371-
row.key(key);
1372-
1373-
GridCacheQueryManager qryMgr = cctx.queries();
1374-
1375-
qryMgr.store(row, null, false);
1376-
}
1377-
}
1378-
13791358
/** {@inheritDoc} */
13801359
@Override public void remove(GridCacheContext cctx, KeyCacheObject key, int partId) throws IgniteCheckedException {
13811360
if (!busyLock.enterBusy())

modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/GridCacheOffheapManager.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,6 @@ private Metas getOrAllocatePartitionMetas() throws IgniteCheckedException {
12351235
delegate.update(cctx, key, val, ver, expireTime, oldRow);
12361236
}
12371237

1238-
/** {@inheritDoc} */
1239-
@Override public void updateIndexes(GridCacheContext cctx, KeyCacheObject key) throws IgniteCheckedException {
1240-
CacheDataStore delegate = init0(false);
1241-
1242-
delegate.updateIndexes(cctx, key);
1243-
}
1244-
12451238
/** {@inheritDoc} */
12461239
@Override public CacheDataRow createRow(
12471240
GridCacheContext cctx,

modules/core/src/main/java/org/apache/ignite/internal/processors/query/GridQueryProcessor.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.apache.ignite.internal.processors.cache.query.GridCacheQueryType;
7474
import org.apache.ignite.internal.processors.cache.query.IgniteQueryErrorCode;
7575
import org.apache.ignite.internal.processors.query.property.QueryBinaryProperty;
76+
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheFilter;
7677
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitor;
7778
import org.apache.ignite.internal.processors.query.schema.SchemaIndexCacheVisitorImpl;
7879
import org.apache.ignite.internal.processors.query.schema.SchemaIndexOperationCancellationToken;
@@ -96,10 +97,12 @@
9697
import org.apache.ignite.internal.util.lang.GridCloseableIterator;
9798
import org.apache.ignite.internal.util.lang.GridClosureException;
9899
import org.apache.ignite.internal.util.lang.IgniteOutClosureX;
100+
import org.apache.ignite.internal.util.tostring.GridToStringExclude;
99101
import org.apache.ignite.internal.util.typedef.F;
100102
import org.apache.ignite.internal.util.typedef.T2;
101103
import org.apache.ignite.internal.util.typedef.T3;
102104
import org.apache.ignite.internal.util.typedef.internal.CU;
105+
import org.apache.ignite.internal.util.typedef.internal.S;
103106
import org.apache.ignite.internal.util.typedef.internal.U;
104107
import org.apache.ignite.internal.util.worker.GridWorker;
105108
import org.apache.ignite.internal.util.worker.GridWorkerFuture;
@@ -1319,12 +1322,15 @@ public void processSchemaOperationLocal(SchemaAbstractOperation op, QueryTypeDes
13191322

13201323
try {
13211324
if (op instanceof SchemaIndexCreateOperation) {
1322-
SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation)op;
1325+
final SchemaIndexCreateOperation op0 = (SchemaIndexCreateOperation)op;
13231326

13241327
QueryIndexDescriptorImpl idxDesc = QueryUtils.createIndexDescriptor(type, op0.index());
13251328

1326-
SchemaIndexCacheVisitor visitor =
1327-
new SchemaIndexCacheVisitorImpl(this, cache.context(), cacheName, op0.tableName(), cancelTok);
1329+
GridCacheContext cctx = cache.context();
1330+
1331+
SchemaIndexCacheFilter filter = new TableCacheFilter(cctx, op0.tableName());
1332+
1333+
SchemaIndexCacheVisitor visitor = new SchemaIndexCacheVisitorImpl(cctx, filter, cancelTok);
13281334

13291335
idx.dynamicIndexCreate(op0.schemaName(), op0.tableName(), idxDesc, op0.ifNotExists(), visitor);
13301336
}
@@ -2869,4 +2875,43 @@ public void manager(SchemaOperationManager mgr) {
28692875
this.mgr = mgr;
28702876
}
28712877
}
2878+
2879+
/** */
2880+
private static class TableCacheFilter implements SchemaIndexCacheFilter {
2881+
/** */
2882+
@GridToStringExclude
2883+
private final GridCacheContext cctx;
2884+
2885+
/** */
2886+
@GridToStringExclude
2887+
private final GridQueryProcessor query;
2888+
2889+
/** */
2890+
private final String cacheName;
2891+
2892+
/** */
2893+
private final String tableName;
2894+
2895+
/**
2896+
* @param cctx Cache context.
2897+
* @param tableName Target table name.
2898+
*/
2899+
TableCacheFilter(GridCacheContext cctx, String tableName) {
2900+
this.cctx = cctx;
2901+
this.tableName = tableName;
2902+
2903+
cacheName = cctx.name();
2904+
query = cctx.kernalContext().query();
2905+
}
2906+
2907+
/** {@inheritDoc} */
2908+
@Override public boolean apply(CacheDataRow row) throws IgniteCheckedException {
2909+
return query.belongsToTable(cctx, cacheName, tableName, row.key(), row.value());
2910+
}
2911+
2912+
/** {@inheritDoc} */
2913+
@Override public String toString() {
2914+
return S.toString(TableCacheFilter.class, this);
2915+
}
2916+
}
28722917
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.processors.query.schema;
19+
20+
import org.apache.ignite.IgniteCheckedException;
21+
import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
22+
23+
/**
24+
* Index row filter accepting current entry.
25+
*/
26+
public interface SchemaIndexCacheFilter {
27+
/**
28+
* @param row Cache data row.
29+
* @return {@code True} if row passes the filter.
30+
* @throws IgniteCheckedException If failed.
31+
*/
32+
boolean apply(CacheDataRow row) throws IgniteCheckedException;
33+
}

0 commit comments

Comments
 (0)