Example #1
0
        private IndexVersion GetIndexVersion(string indexName)
        {
            bool indexVersionExists = ElasticClient.DocumentExists <IndexVersion>(indexName, get => get
                                                                                  .Index(IndexVersionsIndexName)).Exists;

            if (!indexVersionExists)
            {
                var newIndexVersion = new IndexVersion()
                {
                    Name    = indexName,
                    Version = 0
                };

                UpdateIndexVersion(newIndexVersion);

                return(newIndexVersion);
            }
            else
            {
                var getIndexVersionResponse = ElasticClient.Get <IndexVersion>(indexName, get => get
                                                                               .Index(IndexVersionsIndexName));

                return(getIndexVersionResponse.Source);
            }
        }
Example #2
0
        public void Initialize()
        {
            string[] hosts = _hosts.Split(',');
            if (hosts.Length == 0)
            {
                throw new Exception("ElasticSearch hosts list is empty");
            }

            List <Uri> uriList = new List <Uri>();

            hosts.ToList().ForEach(originalHost =>
            {
                string host = originalHost;
                int port    = 9200;

                string[] splittedHost = host.Split(':');
                if (splittedHost.Length > 1)
                {
                    host = splittedHost[0];
                    if (string.IsNullOrEmpty(host))
                    {
                        throw new Exception("ElasticSearch host is invalid");
                    }

                    port = Convert.ToInt32(splittedHost[1]);

                    if (port < 1)
                    {
                        throw new Exception("ElasticSearch host port is invalid");
                    }
                }

                Uri uri = new Uri($"http://{host}:{port}");
                uriList.Add(uri);
            });

            IConnectionPool connectionPool =
                uriList.Count > 1 ?
                (IConnectionPool) new SniffingConnectionPool(uriList) :
                (IConnectionPool) new SingleNodeConnectionPool(uriList[0]);

            var settings = new ConnectionSettings(connectionPool)
                           .DisableAutomaticProxyDetection()
                           .ThrowExceptions();

            ElasticClient = new ElasticClient(settings);

            CreateIndexVersionsIndex();

            string[] indexNames = _elasticSearchDbContext.IndexNames;

            indexNames.ToList().ForEach(indexName =>
            {
                int newestIndexVersion = _elasticSearchDbContext.GetIndexVersion(indexName);

                IndexVersion indexVersion = GetIndexVersion(indexName);

                if (indexVersion.Version < 1)
                {
                    CreateIndexVersion(indexName, 0, newestIndexVersion);
                }
                else
                {
                    if (newestIndexVersion > indexVersion.Version)
                    {
                        CreateIndexVersion(indexName, indexVersion.Version, newestIndexVersion);
                    }
                }
            });
        }
Example #3
0
 private void UpdateIndexVersion(IndexVersion indexVersion)
 {
     ElasticClient.Index(indexVersion, index => index
                         .Index(IndexVersionsIndexName)
                         .Id(indexVersion.Name));
 }
Example #4
0
        private void CreateIndexVersion(string indexName, long oldVersion, int newVersion)
        {
            var createIndexDescriptor = _elasticSearchDbContext.GetCreateIndexDescriptor(this, indexName);

            string oldIndexNameWithVersion = indexName + "_v" + oldVersion;
            string newIndexNameWithVersion = indexName + "_v" + newVersion;

            Console.WriteLine($"Creating index {newIndexNameWithVersion}...");

            createIndexDescriptor = createIndexDescriptor.Index(newIndexNameWithVersion);

            var createIndexResponse = ElasticClient.CreateIndex(newIndexNameWithVersion, index => createIndexDescriptor);

            Console.WriteLine($"Index {newIndexNameWithVersion} created");

            if (oldVersion > 0)
            {
                // we have an old index, reindex

                Console.WriteLine($"Reindexing from {oldIndexNameWithVersion} to {newIndexNameWithVersion}...");

                var reindexOnServerResult = ElasticClient.ReindexOnServer(reindex => reindex
                                                                          .Source(source => source
                                                                                  .Index(oldIndexNameWithVersion)
                                                                                  )
                                                                          .Destination(destination => destination
                                                                                       .Index(newIndexNameWithVersion)
                                                                                       .VersionType(VersionType.Internal)
                                                                                       ));

                Console.WriteLine($"Reindexed from {oldIndexNameWithVersion} to {newIndexNameWithVersion} " +
                                  $"({reindexOnServerResult.Created} created, {reindexOnServerResult.Updated} updated)");
            }

            var aliasExists = ElasticClient.AliasExists(indexName).Exists;

            if (aliasExists)
            {
                // we need to remove the old alias

                Console.WriteLine($"Deleting alias {indexName} -> {oldIndexNameWithVersion}...");

                ElasticClient.DeleteAlias(oldIndexNameWithVersion, indexName);

                Console.WriteLine($"Deleted alias {indexName} -> {oldIndexNameWithVersion}");
            }

            // we need to add the new alias

            Console.WriteLine($"Creating alias {indexName} -> {newIndexNameWithVersion}...");

            var createAliasResponse = ElasticClient.Alias(alias => alias
                                                          .Add(action => action
                                                               .Index(newIndexNameWithVersion)
                                                               .Alias(indexName)
                                                               )
                                                          );

            if (!createAliasResponse.Acknowledged)
            {
                throw new Exception($"Cannot create alias {indexName} -> {newIndexNameWithVersion}");
            }

            Console.WriteLine($"Created alias {indexName} -> {newIndexNameWithVersion}");

            var newIndexVersion = new IndexVersion()
            {
                Name    = indexName,
                Version = newVersion
            };

            UpdateIndexVersion(newIndexVersion);

            if (oldVersion > 0)
            {
                if (!_isProduction)
                {
                    // if everything succeeded, remove the old index

                    // do not delete old index data in production because it is just too dangerous

                    Console.WriteLine($"Deleting old index {oldIndexNameWithVersion}");

                    var deleteIndexResponse = ElasticClient.DeleteIndex(oldIndexNameWithVersion);

                    if (!deleteIndexResponse.Acknowledged)
                    {
                        throw new Exception($"Cannot delete old index {oldIndexNameWithVersion}");
                    }

                    Console.WriteLine($"Deleted old index {oldIndexNameWithVersion}");
                }
                else
                {
                    Console.WriteLine($"WARNING: do not forget to delete old index {oldIndexNameWithVersion} if everything is all right!");
                }
            }
        }
 public void Read (TProtocol iprot)
 {
   TField field;
   iprot.ReadStructBegin();
   while (true)
   {
     field = iprot.ReadFieldBegin();
     if (field.Type == TType.Stop) { 
       break;
     }
     switch (field.ID)
     {
       case 1:
         if (field.Type == TType.I32) {
           CommitTimeSec = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 2:
         if (field.Type == TType.I32) {
           DirectoryType = (DirectoryType)iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 3:
         if (field.Type == TType.I32) {
           DefaultWriteLockTimeout = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 4:
         if (field.Type == TType.I32) {
           RamBufferSizeMb = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 5:
         if (field.Type == TType.I32) {
           RefreshTimeMilliSec = iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       case 6:
         if (field.Type == TType.I32) {
           IndexVersion = (IndexVersion)iprot.ReadI32();
         } else { 
           TProtocolUtil.Skip(iprot, field.Type);
         }
         break;
       default: 
         TProtocolUtil.Skip(iprot, field.Type);
         break;
     }
     iprot.ReadFieldEnd();
   }
   iprot.ReadStructEnd();
 }
 public IndexConfiguration() {
   this._CommitTimeSec = 60;
   this.__isset.CommitTimeSec = true;
   this._DirectoryType = DirectoryType.MemoryMapped;
   this.__isset.DirectoryType = true;
   this._DefaultWriteLockTimeout = 1000;
   this.__isset.DefaultWriteLockTimeout = true;
   this._RamBufferSizeMb = 100;
   this.__isset.RamBufferSizeMb = true;
   this._RefreshTimeMilliSec = 25;
   this.__isset.RefreshTimeMilliSec = true;
   this._IndexVersion = IndexVersion.Lucene47;
   this.__isset.IndexVersion = true;
 }
Example #7
0
 public override string GetPathForIndex() => GetPathHelper(BaseBlobPath, GetForkNumbers(), " Index " + IndexVersion.ToString());