Skip to content

Commit 2970fd3

Browse files
committed
Change index setting to contacts_index and rename some stuff for clarity
1 parent ca472f4 commit 2970fd3

File tree

6 files changed

+42
-45
lines changed

6 files changed

+42
-45
lines changed

Diff for: cmd/rp-indexer/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func main() {
5555
}
5656

5757
idxrs := []indexers.Indexer{
58-
indexers.NewContactIndexer(cfg.ElasticURL, cfg.Index, 500),
58+
indexers.NewContactIndexer(cfg.ElasticURL, cfg.ContactsIndex, 500),
5959
}
6060

6161
if cfg.Rebuild {

Diff for: config.go

+20-19
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@ package indexer
33
import "os"
44

55
type Config struct {
6-
ElasticURL string `help:"the url for our elastic search instance"`
7-
DB string `help:"the connection string for our database"`
8-
Index string `help:"the alias for our contact index"`
9-
Poll int `help:"the number of seconds to wait between checking for updated contacts"`
10-
Rebuild bool `help:"whether to rebuild the index, swapping it when complete, then exiting (default false)"`
11-
Cleanup bool `help:"whether to remove old indexes after a rebuild"`
12-
LogLevel string `help:"the log level, one of error, warn, info, debug"`
13-
SentryDSN string `help:"the sentry configuration to log errors to, if any"`
14-
ContactsShards int `help:"The number of shards to use for the contacts index"`
15-
ContactsReplicas int `help:"The number of replicas to use for the contacts index"`
16-
6+
ElasticURL string `help:"the url for our elastic search instance"`
7+
DB string `help:"the connection string for our database"`
8+
Poll int `help:"the number of seconds to wait between checking for database updates"`
9+
Rebuild bool `help:"whether to rebuild the index, swapping it when complete, then exiting (default false)"`
10+
Cleanup bool `help:"whether to remove old indexes after a rebuild"`
11+
LogLevel string `help:"the log level, one of error, warn, info, debug"`
12+
SentryDSN string `help:"the sentry configuration to log errors to, if any"`
1713
LibratoUsername string `help:"the username that will be used to authenticate to Librato"`
1814
LibratoToken string `help:"the token that will be used to authenticate to Librato"`
1915
InstanceName string `help:"the unique name of this instance used for analytics"`
16+
17+
ContactsIndex string `help:"the alias to use for the contact index"`
18+
ContactsShards int `help:"the number of shards to use for the contacts index"`
19+
ContactsReplicas int `help:"the number of replicas to use for the contacts index"`
2020
}
2121

2222
func NewDefaultConfig() *Config {
2323
hostname, _ := os.Hostname()
2424

2525
return &Config{
26-
ElasticURL: "http://localhost:9200",
27-
DB: "postgres://localhost/temba?sslmode=disable",
28-
Index: "contacts",
29-
Poll: 5,
30-
Rebuild: false,
31-
Cleanup: false,
32-
LogLevel: "info",
33-
InstanceName: hostname,
26+
ElasticURL: "http://localhost:9200",
27+
DB: "postgres://localhost/temba?sslmode=disable",
28+
Poll: 5,
29+
Rebuild: false,
30+
Cleanup: false,
31+
LogLevel: "info",
32+
InstanceName: hostname,
33+
34+
ContactsIndex: "contacts",
3435
ContactsShards: 2,
3536
ContactsReplicas: 1,
3637
}

Diff for: indexers/base.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type Indexer interface {
3333
Stats() Stats
3434
}
3535

36-
type ElasticSettings struct {
36+
type IndexDefinition struct {
3737
Settings struct {
3838
Index struct {
3939
NumberOfShards int `json:"number_of_shards"`
@@ -111,7 +111,7 @@ func (i *baseIndexer) FindIndexes() []string {
111111
// that index to `contacts`.
112112
//
113113
// If the day-specific name already exists, we append a .1 or .2 to the name.
114-
func (i *baseIndexer) createNewIndex(indexSettings ElasticSettings) (string, error) {
114+
func (i *baseIndexer) createNewIndex(def *IndexDefinition) (string, error) {
115115
// create our day-specific name
116116
index := fmt.Sprintf("%s_%s", i.name, time.Now().Format("2006_01_02"))
117117
idx := 0
@@ -133,11 +133,9 @@ func (i *baseIndexer) createNewIndex(indexSettings ElasticSettings) (string, err
133133
}
134134

135135
// create the new index
136-
settings, err := json.Marshal(indexSettings)
137-
if err != nil {
138-
return "", err
139-
}
140-
_, err = utils.MakeJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s?include_type_name=true", i.elasticURL, index), settings, nil)
136+
settings := jsonx.MustMarshal(def)
137+
138+
_, err := utils.MakeJSONRequest(http.MethodPut, fmt.Sprintf("%s/%s?include_type_name=true", i.elasticURL, index), settings, nil)
141139
if err != nil {
142140
return "", err
143141
}

Diff for: indexers/base_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package indexers_test
33
import (
44
"context"
55
"database/sql"
6-
"io/ioutil"
76
"log"
87
"os"
98
"sort"
@@ -22,7 +21,7 @@ const elasticURL = "http://localhost:9200"
2221
const aliasName = "indexer_test"
2322

2423
func setup(t *testing.T) (*sql.DB, *elastic.Client) {
25-
testDB, err := ioutil.ReadFile("../testdb.sql")
24+
testDB, err := os.ReadFile("../testdb.sql")
2625
require.NoError(t, err)
2726

2827
db, err := sql.Open("postgres", "postgres://nyaruka:nyaruka@localhost:5432/elastic_test?sslmode=disable")

Diff for: indexers/contacts.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import (
44
"bytes"
55
"database/sql"
66
_ "embed"
7-
"encoding/json"
87
"fmt"
8+
"time"
9+
10+
"github.com/nyaruka/gocommon/jsonx"
911
"github.com/pkg/errors"
1012
"github.com/sirupsen/logrus"
11-
"time"
1213
)
1314

14-
//go:embed contacts.settings.json
15-
var contactsSettingsFile []byte
16-
17-
var contactsSettings ElasticSettings
15+
//go:embed contacts.index.json
16+
var contactsIndexDefinition []byte
1817

1918
// ContactIndexer is an indexer for contacts
2019
type ContactIndexer struct {
@@ -32,7 +31,7 @@ func NewContactIndexer(elasticURL, name string, batchSize int) *ContactIndexer {
3231
}
3332

3433
// Index indexes modified contacts and returns the name of the concrete index
35-
func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards int, replicas int) (string, error) {
34+
func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards, replicas int) (string, error) {
3635
var err error
3736

3837
// find our physical index
@@ -48,13 +47,13 @@ func (i *ContactIndexer) Index(db *sql.DB, rebuild, cleanup bool, shards int, re
4847

4948
// doesn't exist or we are rebuilding, create it
5049
if physicalIndex == "" || rebuild {
51-
err = json.Unmarshal(contactsSettingsFile, &contactsSettings)
52-
if err != nil {
53-
return "", errors.Wrap(err, "error unmarshalling embeded contacts.settings.json file")
54-
}
55-
contactsSettings.Settings.Index.NumberOfShards = shards
56-
contactsSettings.Settings.Index.NumberOfReplicas = replicas
57-
physicalIndex, err = i.createNewIndex(contactsSettings)
50+
def := &IndexDefinition{}
51+
jsonx.MustUnmarshal(contactsIndexDefinition, def)
52+
53+
def.Settings.Index.NumberOfShards = shards
54+
def.Settings.Index.NumberOfReplicas = replicas
55+
56+
physicalIndex, err = i.createNewIndex(def)
5857
if err != nil {
5958
return "", errors.Wrap(err, "error creating new index")
6059
}

Diff for: indexers/contacts.settings.json renamed to indexers/contacts.index.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"settings": {
33
"index": {
4-
"number_of_shards": 2,
5-
"number_of_replicas": 1,
4+
"number_of_shards": -1,
5+
"number_of_replicas": -1,
66
"routing_partition_size": 1
77
},
88
"analysis": {

0 commit comments

Comments
 (0)