dgraph icon indicating copy to clipboard operation
dgraph copied to clipboard

Crash server when failed to convert facet value

Open munisystem opened this issue 8 years ago • 3 comments

I use api.Faset{} to improve mutation performance with go client. Just as follow.

...
nquads := []*api.NQuad {
  &api.NQuad{
    Subject: "0x01",
    Predicate: "friend",
    ObjectId: "0x02",
    Fasets: []*api.Facet{
      {
        Key: "since",
        Value: []bytes(time.Now().Format(time.RFC3339)),
        ValType: api.Faset_DATETIME,
      },
    },
  },
}
mu := &api.Mutation{Set: nquads, CommitNow: true}
resp, err := txn.Mutate(context.Background(), mu)
...

But this code cause server to crash. ( Valuable of api.Faset.Value is invalid. Actually, should be used time.Now().MarshalBinary()) Because call log.Fatal() when failed to convert facet value.

e.g. https://github.com/dgraph-io/dgraph/blob/5e47b20d60ac54472f573a57cd13414fee8fe531/types/facets/utils.go#L199-L200 https://github.com/dgraph-io/dgraph/blob/02dc3bd36e266a795f78d41679ecdb69c69cff54/x/error.go#L57-L75

I think that should be return error instead of log.Fatal() in public API. How about you?

munisystem avatar Feb 01 '18 11:02 munisystem

This issue is reproducible when querying for a facet as ValFor is only called in the query path. We should have some kind of validation while mutating the facet so that this error is not encountered. To reproduce, run the mutation mentioned above and then query for the facet.

@munisystem I doubt mutation performance would improve much because of this as parsing the facet is not the bottleneck. You should typically use the JSON format to set the facets or the string format (as part of RDF's). Though if you really want to set it like this, I'd suggest using FacetFor so that value is converted to a format the server expects.

pawanrawal avatar Feb 05 '18 03:02 pawanrawal

@munisystem I doubt mutation performance would improve much because of this as parsing the facet is not the bottleneck. You should typically use the JSON format to set the facets or the string format (as part of RDF's).

I guess so. But I have other intention. For example, cannot use JSON in case of that add a user and many friends having facets like https://docs.dgraph.io/mutations/#facets.

this json object doesn't apply facets

{
  "name": "Alice",
  "friend": [
    {
      "name": "Bob",
      "~friend|close": "yes"
    },
    {
      "name": "Carol",
      "~friend|close": "no"
    },
  ]
}

So I have to divide query adding "Alice" and friends or to use string formats. However, above approaches complicate coding and more compared to using JSON...

Though if you really want to set it like this, I'd suggest using FacetFor so that value is converted to a format the server expects.

Sounds good. I try to use it, thanks.

munisystem avatar Feb 07 '18 02:02 munisystem

Try with the JSON below. You don't need to add ~. Reverse edges automatically get the same facets as the forward edges.

{
  "name": "Alice",
  "friend": [
    {
      "name": "Bob",
      "friend|close": "yes"
    },
    {
      "name": "Carol",
      "friend|close": "no"
    },
  ]
}

pawanrawal avatar Feb 07 '18 02:02 pawanrawal