Skip to content

Commit

Permalink
[minor] Merge pull request #2 from rinx/lua-based-config
Browse files Browse the repository at this point in the history
Lua based config feature
  • Loading branch information
rinx authored May 28, 2021
2 parents 27da6d4 + 562476d commit cae3bb0
Show file tree
Hide file tree
Showing 19 changed files with 649 additions and 432 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,21 @@ $ # after Servers become ready, deploy Agents
$ kubectl apply -f k8s/agent.yaml
```
Egress filter feature
Lua based config
---
Instead of using command-line flags, alvd can be configured by using a Lua based config file.
Thare's an example Lua file at [examples/config/config.lua](examples/config/config.lua).

$ ./alvd server --config=examples/config/config.lua

### Egress filter feature

alvd has an egress filter (= post filter) feature (filtering, sorting, translating, etc...) that is extensible by using Lua scripts.

To enable it, run alvd server by passing a path to the Lua scripts.

$ ./alvd server --egress-filter-lua-filepath=examples/egress-filter/sort.lua
$ ./alvd server --config=examples/egress-filter/sort.lua

There're various types of examples of filters are available in [examples/egress-filter](examples/egress-filter) directory.
Expand All @@ -203,8 +210,6 @@ Current Status
Build
---
Just running

$ make cmd/alvd/alvd
Expand Down
51 changes: 51 additions & 0 deletions examples/config/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local json = require("json")
local time = require("time")

agent = {
server_addresses = {"0.0.0.0:8000"},
agent_name = "",
log_level = "info",
dimension = 300,
distance_type = "l2",
object_type = "float",
creation_edge_size = 10,
search_edge_size = 20,
bulk_insert_chunk_size = 100,
index_path = "",
index_self_check_interval = "30m",
grpc_host = "0.0.0.0",
grpc_port = 8081,
metrics_host = "0.0.0.0",
metrics_port = 9090,
metrics_collect_interval = "5s",
}

server = {
agent_enabled = true,
log_level = "info",

server_grpc_host = "0.0.0.0",
server_grpc_port = 8080,

metrics_host = "0.0.0.0",
metrics_port = 9090,
metrics_collect_interval = "5s",

replicas = 2,
check_index_interval = "10s",
create_index_threshold = 1000,

egress_filter = function (results, retry)
for i, r in results() do
results[i].Id = json.encode(
{
id = r.Id,
time = time.format(
time.unix(),
"Jan 2 15:04:05 2006",
"Asia/Tokyo"),
}
)
end
end,
}
13 changes: 7 additions & 6 deletions examples/egress-filter/dump.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-- dump.lua is useful for debugging.
-- `results` is a slice of search results.

for i, r in results() do
print(string.format("Id: %s, Distance: %3.7f", r.Id, r.Distance))
end
server = {
egress_filter = function (results, retry)
for i, r in results() do
print(string.format("Id: %s, Distance: %3.7f", r.Id, r.Distance))
end
end,
}
22 changes: 12 additions & 10 deletions examples/egress-filter/jsonize.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
-- `results` is a slice of search results.

-- using vadv/gopher-lua-libs
local json = require("json")
local time = require("time")

for i, r in results() do
results[i].Id = json.encode(
{
id = r.Id,
time = time.format(time.unix(), "Jan 2 15:04:05 2006", "Asia/Tokyo")
}
)
end
server = {
egress_filter = function (results, retry)
for i, r in results() do
results[i].Id = json.encode(
{
id = r.Id,
time = time.format(time.unix(), "Jan 2 15:04:05 2006", "Asia/Tokyo")
}
)
end
end,
}
28 changes: 15 additions & 13 deletions examples/egress-filter/reduce.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
-- `results` is a slice of search results.
server = {
egress_filter = function (results, retry)
local remains = {}
for i, r in results() do
-- remove elements by distances
if r.Distance < 10.9 then
remains[#remains+1] = r
end

local remains = {}
for i, r in results() do
-- remove elements by distances
if r.Distance < 10.9 then
remains[#remains+1] = r
end
results[i] = nil
end

results[i] = nil
end

for i, r in pairs(remains) do
results[i] = r
end
for i, r in pairs(remains) do
results[i] = r
end
end,
}
38 changes: 21 additions & 17 deletions examples/egress-filter/retryable.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
-- if `retry.Enabled` is true, retry ANN search when the number of `results` is lower than the required number.
retry.Enabled = true
-- `retry.MaxRetries` represents maximum number of retries.
retry.MaxRetries = 3
-- `retry.NextNumMultiplier` represents how to increase number of internal search results.
retry.NextNumMultiplier = 2
server = {
egress_filter = function (results, retry)
-- if `retry.Enabled` is true, retry ANN search when the number of `results` is lower than the required number.
retry.Enabled = true
-- `retry.MaxRetries` represents maximum number of retries.
retry.MaxRetries = 3
-- `retry.NextNumMultiplier` represents how to increase number of internal search results.
retry.NextNumMultiplier = 2

local remains = {}
for i, r in results() do
-- remove elements if ID lengths is lower than 3
if string.len(r.Id) >= 3 then
remains[#remains+1] = r
end
local remains = {}
for i, r in results() do
-- remove elements if ID lengths is lower than 3
if string.len(r.Id) >= 3 then
remains[#remains+1] = r
end

results[i] = nil
end
results[i] = nil
end

for i, r in pairs(remains) do
results[i] = r
end
for i, r in pairs(remains) do
results[i] = r
end
end,
}
12 changes: 7 additions & 5 deletions examples/egress-filter/reverse-id.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-- `results` is a slice of search results.

for i, r in results() do
results[i].Id = string.reverse(r.Id)
end
server = {
egress_filter = function (results, retry)
for i, r in results() do
results[i].Id = string.reverse(r.Id)
end
end,
}
33 changes: 17 additions & 16 deletions examples/egress-filter/sort.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
-- `results` is a slice of search results.
-- to apply `table.sort`, put the data into `sorter`
server = {
egress_filter = function (results, retry)
local sorter = {}
for i, r in results() do
sorter[i] = r
end

local sorter = {}
for i, r in results() do
sorter[i] = r
end
-- sort
table.sort(sorter, function(a, b)
-- reverse order
return a.Distance > b.Distance
end)

-- sort
table.sort(sorter, function(a, b)
-- reverse order
return a.Distance > b.Distance
end)

-- put the sorted data into `results`
for i, r in pairs(sorter) do
results[i] = r
end
-- put the sorted data into `results`
for i, r in pairs(sorter) do
results[i] = r
end
end,
}
83 changes: 44 additions & 39 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,52 @@ module github.com/rinx/alvd
go 1.16

replace (
cloud.google.com/go => cloud.google.com/go v0.81.0
github.com/Azure/go-autorest => github.com/Azure/go-autorest v14.2.0+incompatible
github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.38.21
cloud.google.com/go => cloud.google.com/go v0.82.1-0.20210525054938-8bd87cbfee00
cloud.google.com/go/storage => cloud.google.com/go/storage v1.15.1-0.20210525054938-8bd87cbfee00
github.com/Azure/go-autorest => github.com/Azure/go-autorest v14.2.1-0.20210210161804-c7f947c0610d+incompatible
github.com/aws/aws-sdk-go => github.com/aws/aws-sdk-go v1.38.46
github.com/boltdb/bolt => github.com/boltdb/bolt v1.3.1
github.com/chzyer/logex => github.com/chzyer/logex v1.1.11-0.20170329064859-445be9e134b2
github.com/coreos/etcd => go.etcd.io/etcd v3.3.25+incompatible
github.com/docker/docker => github.com/moby/moby v20.10.6+incompatible
github.com/envoyproxy/protoc-gen-validate => github.com/envoyproxy/protoc-gen-validate v0.5.1
github.com/envoyproxy/protoc-gen-validate => github.com/envoyproxy/protoc-gen-validate v0.6.1
github.com/go-sql-driver/mysql => github.com/go-sql-driver/mysql v1.6.0
github.com/gocql/gocql => github.com/gocql/gocql v0.0.0-20210413161705-87a5d7a5ff74
github.com/gocql/gocql => github.com/gocql/gocql v0.0.0-20210515062232-b7ef815b4556
github.com/gogo/googleapis => github.com/gogo/googleapis v1.4.1
github.com/gogo/protobuf => github.com/gogo/protobuf v1.3.2
github.com/google/go-cmp => github.com/google/go-cmp v0.5.5
github.com/google/pprof => github.com/google/pprof v0.0.0-20210413054141-7c2eacd09c8d
github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.5.4
github.com/google/go-cmp => github.com/google/go-cmp v0.5.6
github.com/google/pprof => github.com/google/pprof v0.0.0-20210506205249-923b5ab0fc1a
github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.5.5
github.com/gophercloud/gophercloud => github.com/gophercloud/gophercloud v0.17.0
github.com/gorilla/websocket => github.com/gorilla/websocket v1.4.2
github.com/hailocab/go-hostpool => github.com/kpango/go-hostpool v0.0.0-20210303030322-aab80263dcd0
github.com/klauspost/compress => github.com/klauspost/compress v1.12.1
github.com/klauspost/compress => github.com/klauspost/compress v1.12.3-0.20210521105228-b5e94846a1d8
github.com/kpango/glg => github.com/kpango/glg v1.5.1
github.com/tensorflow/tensorflow => github.com/tensorflow/tensorflow v2.1.2+incompatible
golang.org/x/crypto => golang.org/x/crypto v0.0.0-20210415154028-4f45737414dc
google.golang.org/grpc => google.golang.org/grpc v1.37.0
golang.org/x/crypto => golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
google.golang.org/grpc => google.golang.org/grpc v1.38.0
google.golang.org/protobuf => google.golang.org/protobuf v1.26.0
k8s.io/api => k8s.io/api v0.20.4
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.20.4
k8s.io/apimachinery => k8s.io/apimachinery v0.20.4
k8s.io/cli-runtime => k8s.io/cli-runtime v0.20.4
k8s.io/client-go => k8s.io/client-go v0.20.4
k8s.io/metrics => k8s.io/metrics v0.20.4
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.8.2
k8s.io/api => k8s.io/api v0.20.7
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.20.7
k8s.io/apimachinery => k8s.io/apimachinery v0.20.7
k8s.io/cli-runtime => k8s.io/cli-runtime v0.20.7
k8s.io/client-go => k8s.io/client-go v0.20.7
k8s.io/metrics => k8s.io/metrics v0.20.7
sigs.k8s.io/controller-runtime => sigs.k8s.io/controller-runtime v0.8.3
)

require (
cloud.google.com/go v0.81.0
code.cloudfoundry.org/bytefmt v0.0.0-20200131002437-cf55d5288a48
cloud.google.com/go v0.82.0
cloud.google.com/go/storage v1.15.0
code.cloudfoundry.org/bytefmt v0.0.0-20210524144015-27119551aaea
contrib.go.opencensus.io/exporter/jaeger v0.2.1
contrib.go.opencensus.io/exporter/prometheus v0.3.0
contrib.go.opencensus.io/exporter/stackdriver v0.13.5
github.com/aws/aws-sdk-go v1.32.1
contrib.go.opencensus.io/exporter/stackdriver v0.13.6
github.com/aws/aws-sdk-go v1.38.35
github.com/cespare/xxhash/v2 v2.1.1
github.com/fsnotify/fsnotify v1.4.9
github.com/go-redis/redis/v8 v8.8.0
github.com/go-sql-driver/mysql v1.5.0
github.com/go-redis/redis/v8 v8.9.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gocql/gocql v0.0.0-20200131111108-92af2e088537
github.com/gocraft/dbr/v2 v2.7.1
github.com/gogo/googleapis v0.0.0-20180223154316-0cd9801be74a
Expand All @@ -55,8 +57,8 @@ require (
github.com/google/go-cmp v0.5.5
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-version v1.3.0
github.com/json-iterator/go v1.1.10
github.com/klauspost/compress v0.0.0-00010101000000-000000000000
github.com/json-iterator/go v1.1.11
github.com/klauspost/compress v1.12.2
github.com/kpango/fastime v1.0.16
github.com/kpango/fuid v0.0.0-20210407064122-2990e29e1ea5
github.com/kpango/gache v1.2.5
Expand All @@ -67,24 +69,27 @@ require (
github.com/tensorflow/tensorflow v0.0.0-00010101000000-000000000000
github.com/urfave/cli/v2 v2.3.0
github.com/vadv/gopher-lua-libs v0.1.2
github.com/vdaas/vald v1.0.5
github.com/vdaas/vald v1.1.0
github.com/yuin/gluamapper v0.0.0-20150323120927-d836955830e7
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da
go.opencensus.io v0.23.0
go.opentelemetry.io/otel v0.19.0
go.opentelemetry.io/otel/exporters/metric/prometheus v0.19.0
go.opentelemetry.io/otel/metric v0.19.0
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/exporters/metric/prometheus v0.20.0
go.opentelemetry.io/otel/metric v0.20.0
go.uber.org/automaxprocs v1.4.0
go.uber.org/goleak v1.1.10
go.uber.org/zap v1.16.0
golang.org/x/net v0.0.0-20210415231046-e915ea6b2b7d
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c
google.golang.org/api v0.44.0
google.golang.org/grpc v1.36.1
go.uber.org/zap v1.17.0
gocloud.dev v0.23.0
golang.org/x/net v0.0.0-20210525063256-abc453219eb5
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea
google.golang.org/api v0.47.0
google.golang.org/grpc v1.38.0
gopkg.in/yaml.v2 v2.4.0
inet.af/netaddr v0.0.0-20210414225854-daf4c9bcf1f9 // indirect
k8s.io/api v0.20.5
k8s.io/apimachinery v0.20.5
k8s.io/client-go v0.20.5
inet.af/netaddr v0.0.0-20210526175434-db50905a50be
k8s.io/api v0.20.7
k8s.io/apimachinery v0.20.7
k8s.io/client-go v0.20.7
k8s.io/metrics v0.0.0-00010101000000-000000000000
layeh.com/gopher-luar v1.0.8
sigs.k8s.io/controller-runtime v0.0.0-00010101000000-000000000000
Expand Down
Loading

0 comments on commit cae3bb0

Please sign in to comment.