21 Commits

Author SHA1 Message Date
Stephan Renatus 5ef98c7493 store+runtime: extension points for custom stores (#7779)
* storage: allow overriding NonEmpty

Custom store implementations can now bring their own NonEmpty() methods,
which may be more efficient than what the generic method does.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>

* runtime: allow passing in custom store builder


Signed-off-by: Stephan Renatus <stephan@styra.com>

---------

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
Signed-off-by: Stephan Renatus <stephan@styra.com>
2025-07-17 17:04:45 +00:00
Johan Fylling a179a24c48 v1 API
All packages, except for `cmd` and `internal`, have been moved into a new `v1` root package.

Old packages are kept for backwards-compatibility reasons. All contained code is replaced with simple type aliases and proxy functions to `v1` implementations.

Old packages default to the Rego v0 syntax, new `v1` packages default to the Rego v1 syntax.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:27:34 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00
Ashutosh Narkar 570c09359e storage: Handle multi-bundle data with overlapping roots
If the bundles being activated share a manifest root prefix, it
would result in overwriting the bundle data based on the activation
order. This happened since the truncate call writes data to the
store based on the top-level keys in the data. When multiple
bundles with overlapping bundle root prefixes are being activated
as part of the same txn, adding data to the store by iterating
over the top-level keys in the data object would result in an unintended
overwrite. The truncate call would be able to properly write
data if it had knowledge of the bundle roots. This commit passes
the bundle roots to the truncate call to assist in writing data
to the store.

Fixes: #4998

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2022-08-17 10:04:54 -07:00
Ashutosh Narkar 8f63046fb9 storage+bundle: Clean old bundle data before new activation (#4944)
If OPA has an activated bundle that owns all roots
and a new bundle with empty roots is to be activated, the
old bundle's data should first be erased from the store.
Currently both the old and new data is kept in the store.

This commit attempts to fix this by providing an indication to
the truncate call about the scenario in which the root is to be
overwritten.

Fixes: #4940

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2022-07-29 08:08:40 +02:00
Ashutosh Narkar f60dfafa1b Initial support for large bundle deployments
Currently bundles are loaded into memory entirely
even when disk storage is used. Then the parsed content
is written to the store. Deserializing data into Go structs
is memory consuming and even if user has configured disk
storage, OPA is still bound by the amount of memory
assigned to it. This change adds a new lazy loading mode
wherein the entire data is not deserialized while bundle
reading and hence if the bundle contains large data files
and the user has enabled disk storage, OPA should be
able to handle this scenario w/o running OOM.

Fixes: #4539

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2022-06-27 08:51:21 -07:00
Stephan Renatus 516dd47dd1 runtime+storage: integrate disk storage
With this change, the disk backend (badger) becomes available for
use with the OPA runtime properly:

It can be configured using the `storage.disk` key in OPA's config
(see included documentation).

When enabled,
- any data or policies stored with OPA will persist over restarts
- per-query metrics related to disk usage are reported
- Prometheus metrics per storage operation are exported

The main intention behind this feature is to optimize memory usage:
OPA can now operate on more data than fits into the allotted memory
resources. It is NOT meant to be used as a primary source of truth:
there are no backup/restore or desaster recovery procedures -- you
MUST secure the means to restore the data stored with OPA's disk
storage by yourself.

See also #4014. Future improvements around bundle loading are
planned.

Some notes on details:

storage/disk: impose same locking regime used with inmem

With this setup, we'll ensure:

- there is only one open write txn at a time
- there are any number of open read txns at a time
- writes are blocked when reads are inflight
- during a commit (and triggers being run), no read txns can be created

This is to ensure the same atomic policy update semantics when using
'disk" as we have with "inmem". We're basically opting out of badger's
currency control and transactionality guarantees. This is because we
cannot piggy back on that to ensure the atomic update we want.

There might be other ways -- using subscribers, and blocking in some
other place -- but this one seems preferrable since it mirrors inmem.

Part of the problem is ErrTxnTooLarge, and committing and renewing
txns when it occurs: that, which is the prescribed solution to txns
growing too big, also means that reads can see half of the "logical"
transaction having been committed, while the rest is still getting
processed.

Another approach would have been using `WriteBatch`, but that won't
let us read from the batch, only apply Set and Delete operations.
We currently need to read (via an iterator) to figure out if we
need to delete keys to replace something in the store.  There is
no DropPrefix operation on the badger txn, or the WriteBatch API.

storage/disk: remove commit-and-renew-txn code for txn-too-big errors

This would break transactional guarantees we care about: while there
can be only one write transaction at a time, read transactions may
happen while a write txn is underway -- with this commit-and-reset
logic, those would read partial data.

Now, the error will be returned to the caller. The maximum txn size
depends on the size of memtables, and could be tweaked manually.
In general, the caller should try to push multiple smaller increments
of the data.

storage/disk: implement noop MakeDir

The MakeDir operation as implemented in the backend-agnostic storage
code has become an issue with the disk store: to write /foo/bar/baz,
we'd have to read /foo (among other subdirs), and that can be _much_
work for the disk backend. With inmem, it's cheap, so this wasn't
problematic before.

Some of the storage/disk/txn.go logic had to be adjusted to properly
do the MakeDir steps implicitly.

The index argument addition to patch() in storage/disk/txn.go was
necessary to keep the error messages conforming to the previous
code path: previously, conflicts (arrays indexed as objects) would
be surfaced in the MakeDir step, now it's entangled with the patch
calculation.

storage/disk: check ctx.Err() in List/Get operations

This won't abort reading a single key, but it will abort iterations.

storage/disk: support patterns in partitions

There is a potential clash here: "*", the path wildcard, is
a valid path section. However, it only affects the case when
a user would want to have a partition at

    /foo/*/bar

and would really mean "*", and not the wildcard.

Storing data at /foo/*/bar with a literal "*" won't be treated
differently than storing something at /fo/xyz/bar.

storage/disk: keep per-txn-type histograms of stats

This is done by reading off the metrics on commit, and shovelling
their numbers into the prometheus collector.

NOTE: if you were to share a metrics object among multiple transactions,
the results would be skewed, as it's not reset. However, our server
handlers don't do that.

storage/disk: opt out of badger's conflict detection

With only one write transaction in flight at any time, the situation
that badger guards against cannot happen:

A transaction has written to a key after the current, to-be-committed
transaction has last read that key from the store.

Since it can't happen, we can ignore the bookkeeping involved. This
improves the time it takes to overwrite existing keys.

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-03-30 10:25:45 +02:00
Stephan Renatus fc49420287 storage: allow implementations to override MakeDir
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-03-30 10:25:45 +02:00
Stephan Renatus ac7bb1fa70 storage: code cosmetics
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2022-03-30 10:25:45 +02:00
Torin Sandall 97cdaf9f60 storage: Remove unused indexing interface
At one point the in-memory store implemented an indexing strategy so
that variable bindings could be returned for non-ground references to
base documents. However, we eventually disabled in-memory indexing
and we have not re-added it since
3ebbeede6c. At this point, indexing can
be removed completely.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2020-07-21 11:01:58 -04:00
Torin Sandall 7e5c14f255 storage: Add context parameter to transactions and triggers
These changes update the storage to accept arbitrary key/value
parameters when creating transactions. The container is passed to
trigger callbacks that are invoked when transactions commit.

The use case for this is avoiding parse and compile operations on
modules loaded out of bundles. The parse and compile step on large
bundles can take several seconds. Since the triggers are executed
while the store's read-lock is held, policies queries block.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-06-24 11:57:51 -07:00
Stephan Renatus 2f1526c672 fix misspell
Signed-off-by: Stephan Renatus <srenatus@chef.io>
2018-06-05 09:50:13 -07:00
Matthew Mussomele 0b7e33661d Refactor Store interface to eliminate IDs for Trigger registration
The store used to require the user to supply a unique ID in order to
register a trigger. Instead, when registering a trigger, the Store now
returns a handle on which the trigger can be unregistered. This has a
number of benefits, including that the user no longer has to reason
about what IDs they have already used, and only the owner of the handle
can end the trigger.
2017-07-20 17:07:51 -07:00
Matthew Mussomele 5944e598ff Clarify that transactions are aborted when Committing fails
The Store interface was not clear if transactions need to be aborted
if committing them fails. This clarifies that a failed commit must be
equivalent to an abort.
2017-07-20 17:07:51 -07:00
Matthew Mussomele 7d3d379236 Augment Store triggers to provide more detailed information
Previously store triggers only told you whether data changed or
policy changed, they did not specify what data/policy changed or
how they changed. Now, TriggerEvents contain lists describing
all changes to policies and data that were made in a given commit.
2017-07-20 17:07:51 -07:00
Torin Sandall c786fc9d33 Add support for concurrent r/w txns
These changes update the storage layer to support
multi-reader/single-writer txns:

- Writers can read their own writes
- Writers can rollback changes
- Readers only see writes after a successful commit
- Readers can progress during a write txn and only block during a commit

These changes also refactor the trigger interface to better support
transactions.
2017-06-23 13:18:15 -07:00
Torin Sandall 10f22906b5 Refactor storage layer interfaces
This is a large change set that contains a few backwards incompatible
changes. Summary of breaking changes:

- Remove storage.Storage in favour of storage.Store interface.
- Remove mount support.
- Remove storage of compiled policies.
- Modify storage.Store to support rollback.
- Modify storage.Store to support raw policy storage.
- Modify storage.Store to support indexing.
2017-06-23 13:18:15 -07:00
Torin Sandall c3d1542754 Update storage to support context.Context
This includes all of the changes to plumb the context through from the HTTP
server and the REPL.

Also, this removes the Travis CI build for Go 1.6 as the context package is
not part of the standard library before Go 1.7. Once Go 1.8 is released we can
go back to supporting the previous Go release.

Fixes #155
2016-12-07 14:18:15 -08:00
Torin Sandall 26cd8e59ec Updates to use new storage.Path type
These changes refactor the storage layer to use storage.Path instead of ast.Ref
for Read/Write/Begin/Unmount/Mount operations.

Previously, the storage layer used ast.Ref values to refer to locations in
storage. Use of ast.Ref introduced unnecessary complexity for storage plugins
as they had to be aware of various details (e.g., array indices specified as
ast.Number/float64 values, potentially nested references, etc.) that were
unnecessary given that ast.Ref values passed to the storage layer were
intended represent JSON pointers.

These changes also remove the need for storage plugins to be aware of where
they are mounted. That is, the paths passed to the read call will be relative
to the mount point.

The remaining dependencies on the ast package from the storage package are for
(1) indexing and (2) policy storage. It may be possible to further decouple
these packages by revisiting how indexing is done and treating policies as
blobs.

Fixes #159
2016-12-02 10:29:33 -08:00
Torin Sandall 247362606b Update storage interfaces to support writes 2016-08-24 11:30:45 -07:00
Torin Sandall 9ad2f63212 Initial support for pluggable storage backends
These changes add support for pluggable storage backends. Previously, the
storage layer only supported a single, built-in, in-memory storage backend.
Users integrating with OPA would write a shim layer that pushed data into this
backend so that the policy engine could evaluate over it. This approach is not
sufficient when the dataset is too large to replicate or the process of
replication would introduce race conditions into systems integrated with OPA.

With these changes, users can implement their own storage backends for OPA. The
storage backends are "mounted" into the root document at a particular
location. References to data under this location are then served by the
user's storage backend. This allows the storage backend to fetch data from the
source of truth thereby avoiding race conditions (when that is important).

These changes introduce new interfaces in the storage layer:

- Transaction interface
- Store interface (i.e., the pluggable backend)
- Storage object, represents the policy engine's storage layer
- Trigger interface
2016-08-19 09:57:18 -07:00