Skip to content

Commit

Permalink
RavenDB-17793 : use binary search for lookups in prefixed list
Browse files Browse the repository at this point in the history
  • Loading branch information
aviv86 committed Jan 16, 2024
1 parent ad34f96 commit 2650170
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,22 @@ public async Task ExecuteMoveDocuments()
[RavenAction("/databases/*/admin/sharding/prefixes/add", "POST", AuthorizationStatus.DatabaseAdmin)]
public Task AddPrefixConfiguration()
{
throw new NotSupportedInShardingException($"Database '{DatabaseName}' is a sharded database and does not support documents migration operation. " +
"This operation is available only from a specific shard");
throw new NotSupportedInShardingException("This operation is not available from a specific shard");
}

[RavenAction("/databases/*/admin/sharding/prefixes/delete", "DELETE", AuthorizationStatus.DatabaseAdmin)]
public Task DeletePrefixConfiguration()
{
throw new NotSupportedInShardingException($"Database '{DatabaseName}' is a sharded database and does not support documents migration operation. " +
"This operation is available only from a specific shard");
throw new NotSupportedInShardingException("This operation is not available from a specific shard");

}

[RavenAction("/databases/*/admin/sharding/prefixes/update", "POST", AuthorizationStatus.DatabaseAdmin)]
public Task UpdatePrefixConfiguration()
{
throw new NotSupportedInShardingException($"Database '{DatabaseName}' is a sharded database and does not support documents migration operation. " +
"This operation is available only from a specific shard");


throw new NotSupportedInShardingException("This operation is not available from a specific shard");
}


private void ValidateShardDatabaseName()
{
if (ShardHelper.IsShardName(DatabaseName) == false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -37,18 +36,14 @@ public async Task AddPrefixConfiguration()
{
var json = await context.ReadForMemoryAsync(RequestBodyStream(), GetType().Name);
var setting = JsonDeserializationCluster.PrefixedShardingSetting(json);
setting.Prefix = setting.Prefix.ToLower();

var shardingConfiguration = ServerStore.Cluster.ReadShardingConfiguration(DatabaseName);
ShardingStore.AssertValidPrefix(setting, shardingConfiguration);

foreach (var value in shardingConfiguration.Prefixed)
{
if (value.Prefix == setting.Prefix)
throw new InvalidOperationException(
$"Prefix '{setting.Prefix}' already exists in {nameof(ShardingConfiguration)}.{nameof(ShardingConfiguration.Prefixed)}. please use '{nameof(UpdatePrefixedShardingSettingOperation)} operation'");
}

var exists = shardingConfiguration.Prefixed.BinarySearch(setting, PrefixedSettingComparer.Instance) >= 0;
if (exists)
throw new InvalidOperationException(
$"Prefix '{setting.Prefix}' already exists in {nameof(ShardingConfiguration)}.{nameof(ShardingConfiguration.Prefixed)}. please use '{nameof(UpdatePrefixedShardingSettingOperation)} operation'");

var clusterTopology = ServerStore.GetClusterTopology(context);
var urls = shardingConfiguration.Orchestrator.Topology.Members.Select(clusterTopology.GetUrlFromTag).ToArray();
Expand Down Expand Up @@ -77,7 +72,7 @@ public async Task DeletePrefixConfiguration()
var prefix = GetStringQueryString("prefix");

var shardingConfiguration = ServerStore.Cluster.ReadShardingConfiguration(DatabaseName);
bool found = shardingConfiguration.Prefixed.Any(value => value.Prefix == prefix);
bool found = shardingConfiguration.Prefixed.Any(value => string.Equals(value.Prefix, prefix, StringComparison.OrdinalIgnoreCase));
if (found == false)
throw new InvalidDataException($"Prefix '{prefix}' wasn't found in sharding configuration");

Expand Down Expand Up @@ -159,6 +154,5 @@ private async Task<bool> AssertNoDocsStartingWith(TransactionOperationContext co
return command.Result.Results.Length == 0;
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public override void UpdateDatabaseRecord(DatabaseRecord record, long etag)

if (record.Sharding.Shards.ContainsKey(ShardNumber))
throw new RachisApplyException($"Cannot add new shard {ShardNumber} to the database {DatabaseName} because it already exists.");

record.Sharding.Shards.Add(ShardNumber, Topology);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Raven.Client.ServerWide;
using Raven.Client.ServerWide.Sharding;
using Raven.Server.ServerWide.Sharding;
using Raven.Server.Utils;
using Sparrow.Json.Parsing;

Expand All @@ -23,19 +25,10 @@ public UpdatePrefixedSettingCommand(PrefixedShardingSetting setting, string data

public override void UpdateDatabaseRecord(DatabaseRecord record, long etag)
{
PrefixedShardingSetting oldSetting = null;

foreach (var value in record.Sharding.Prefixed)
{
if (value.Prefix != Setting.Prefix)
continue;

oldSetting = value;
}

if (oldSetting == null)
throw new InvalidOperationException($"Prefixed setting '{Setting.Prefix}' was not found in sharding configuration");
var location = record.Sharding.Prefixed.BinarySearch(Setting, PrefixedSettingComparer.Instance);
Debug.Assert(location >= 0, $"Prefixed setting '{Setting.Prefix}' was not found in sharding configuration");

var oldSetting = record.Sharding.Prefixed[location];
int index = 0;
for (; index < record.Sharding.BucketRanges.Count; index++)
{
Expand Down Expand Up @@ -68,7 +61,6 @@ public override void UpdateDatabaseRecord(DatabaseRecord record, long etag)
record.Sharding.BucketRanges.InsertRange(index, newRanges);

oldSetting.Shards = Setting.Shards;

}

public override void FillJson(DynamicJsonValue json)
Expand Down
1 change: 0 additions & 1 deletion src/Raven.Server/ServerWide/ShardingStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ private static void AddPrefixedBucketRange(PrefixedShardingSetting setting, int
AssertValidPrefix(setting, shardingConfiguration);

setting.BucketRangeStart = rangeStart;
setting.Prefix = setting.Prefix.ToLower();

var shards = setting.Shards;
var step = ShardHelper.NumberOfBuckets / shards.Count;
Expand Down

0 comments on commit 2650170

Please sign in to comment.