From d8a417f657a5eb0b6538b91d3d9959693156c8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Podg=C3=B3rski?= Date: Fri, 29 May 2020 16:08:20 +0200 Subject: [PATCH] Move applyCreatedAt from mergeClusterStatus directly to ClusterStatus message, to avoid deadlocks --- api.go | 17 ++++++++--------- cluster.go | 3 --- pilosa.go | 2 +- server.go | 6 ++++++ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/api.go b/api.go index 54bb8f3be..08df4889c 100644 --- a/api.go +++ b/api.go @@ -181,15 +181,17 @@ func (api *API) CreateIndex(ctx context.Context, indexName string, options Index if err != nil { return nil, errors.Wrap(err, "creating index") } + + createdAt := timestamp() index.mu.Lock() - index.createdAt = timestamp() + index.createdAt = createdAt index.mu.Unlock() // Send the create index message to all nodes. err = api.server.SendSync( &CreateIndexMessage{ Index: indexName, - CreatedAt: index.CreatedAt(), + CreatedAt: createdAt, Meta: &options, }) if err != nil { @@ -274,15 +276,16 @@ func (api *API) CreateField(ctx context.Context, indexName string, fieldName str if err != nil { return nil, errors.Wrap(err, "creating field") } + createdAt := timestamp() field.mu.Lock() - field.createdAt = timestamp() + field.createdAt = createdAt field.mu.Unlock() // Send the create field message to all nodes. err = api.server.SendSync(&CreateFieldMessage{ Index: indexName, Field: fieldName, - CreatedAt: field.CreatedAt(), + CreatedAt: createdAt, Meta: &fo, }) if err != nil { @@ -836,11 +839,7 @@ func (api *API) ApplySchema(ctx context.Context, s *Schema, remote bool) error { } } - if err := api.holder.applySchema(s); err != nil { - return errors.Wrap(err, "applying schema") - } - - return nil + return errors.Wrap(api.holder.applySchema(s), "applying schema") } // Views returns the views in the given field. diff --git a/cluster.go b/cluster.go index ced55e67a..2c50bceb9 100644 --- a/cluster.go +++ b/cluster.go @@ -2214,9 +2214,6 @@ func (c *cluster) mergeClusterStatus(cs *ClusterStatus) error { } } - if cs.Schema != nil { - c.holder.applyCreatedAt(cs.Schema.Indexes) - } c.unprotectedSetState(cs.State) c.markAsJoined() diff --git a/pilosa.go b/pilosa.go index 060141467..53733d9ce 100644 --- a/pilosa.go +++ b/pilosa.go @@ -205,7 +205,7 @@ func stringSlicesAreEqual(a, b []string) bool { } func timestamp() int64 { - return time.Now().UTC().UnixNano() + return time.Now().UnixNano() } // AddressWithDefaults converts addr into a valid address, diff --git a/server.go b/server.go index 8684a6061..9fbb30811 100644 --- a/server.go +++ b/server.go @@ -770,6 +770,12 @@ func (s *Server) receiveMessage(m Message) error { if err != nil { return err } + if !s.isCoordinator { + if obj.Schema != nil { + s.holder.applyCreatedAt(obj.Schema.Indexes) + } + } + case *ResizeInstruction: err := s.cluster.followResizeInstruction(obj) if err != nil {