Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix(elasticsearch): handle closed indices with null size/count values (#18869)

Vincent Huang444fdf9ec42c
diff --git a/backend/plugin/db/elasticsearch/elasticsearch.go b/backend/plugin/db/elasticsearch/elasticsearch.go
index 5275c84b8c..53e298c9c8 100644
--- a/backend/plugin/db/elasticsearch/elasticsearch.go
+++ b/backend/plugin/db/elasticsearch/elasticsearch.go
@@ -354,9 +354,14 @@ func (d *Driver) Ping(_ context.Context) error {
// Use Elasticsearch client
if d.typedClient != nil {
- if _, err := d.typedClient.Ping(); err != nil {
+ res, err := d.typedClient.Ping()
+ if err != nil {
return errors.Wrapf(err, "failed to ping db")
}
+ defer res.Body.Close()
+ if res.IsError() {
+ return errors.Errorf("ping failed: %s", res.String())
+ }
return nil
}
diff --git a/backend/plugin/db/elasticsearch/sync.go b/backend/plugin/db/elasticsearch/sync.go
index 39d8687ce0..c2222c57ea 100644
--- a/backend/plugin/db/elasticsearch/sync.go
+++ b/backend/plugin/db/elasticsearch/sync.go
@@ -166,22 +166,18 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {
}
for _, m := range results {
- // index size.
datasize, err := unitConversion(m.IndexSize)
if err != nil {
return nil, err
}
-
- // document count.
- docCount, err := strconv.Atoi(m.DocsCount)
+ docCount, err := parseDocCount(m.DocsCount)
if err != nil {
return nil, err
}
-
indicesMetadata = append(indicesMetadata, &storepb.TableMetadata{
Name: m.Index,
DataSize: datasize,
- RowCount: int64(docCount),
+ RowCount: docCount,
})
}
return indicesMetadata, nil
@@ -216,32 +212,47 @@ func (d *Driver) getIndices() ([]*storepb.TableMetadata, error) {
}
for _, m := range results {
- // index size.
datasize, err := unitConversion(m.IndexSize)
if err != nil {
return nil, err
}
-
- // document count.
- docCount, err := strconv.Atoi(m.DocsCount)
+ docCount, err := parseDocCount(m.DocsCount)
if err != nil {
return nil, err
}
-
indicesMetadata = append(indicesMetadata, &storepb.TableMetadata{
Name: m.Index,
DataSize: datasize,
- RowCount: int64(docCount),
+ RowCount: docCount,
})
}
return indicesMetadata, nil
}
+// parseDocCount parses document count string, returning 0 for empty values (e.g., closed indices).
+func parseDocCount(s string) (int64, error) {
+ if s == "" {
+ return 0, nil
+ }
+ count, err := strconv.Atoi(s)
+ return int64(count), err
+}
+
func unitConversion(sizeWithUnit string) (int64, error) {
+ // Empty size is expected for closed indices (ES returns null → unmarshals to "")
+ if sizeWithUnit == "" {
+ return 0, nil
+ }
+
sizeWithUnit = strings.ToLower(sizeWithUnit)
sizeRe := regexp.MustCompile("([0-9.]+)([gmk]?b)")
match := sizeRe.FindSubmatch([]byte(sizeWithUnit))
+ // Non-empty string that doesn't match expected format is unexpected
+ if len(match) < 3 {
+ return 0, errors.Errorf("unexpected size format: %q", sizeWithUnit)
+ }
+
unit := string(match[2])
size, err := strconv.ParseFloat(string(match[1]), 64)