cofacts / rumors-db

Scripts for managing rumors db
MIT License
5 stars 11 forks source link

add empty list for missing articleCategories field #40

Closed darkbtf closed 4 years ago

darkbtf commented 4 years ago

Fix this error: old version of articleCategories is null, instead of being initialized to empty list.

{
  "errors": [
    {
      "message": "illegal_argument_exception",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "CreateArticleCategory"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "name": "ResponseError",
          "meta": {
            "body": {
              "error": {
                "root_cause": [
                  {
                    "type": "remote_transport_exception",
                    "reason": "[VxKgRte][127.0.0.1:9300][indices:data/write/update[s]]"
                  }
                ],
                "type": "illegal_argument_exception",
                "reason": "failed to execute script",
                "caused_by": {
                  "type": "script_exception",
                  "reason": "runtime error",
                  "script_stack": [
                    "found = ctx._source.articleCategories.stream()\n            .filter(",
                    "                                     ^---- HERE"
                  ],
                  "script": "\n          def found = ctx._source.articleCategories.stream()\n            .filter(ar -> ar.get('categoryId').equals(params.articleCategory.get('categoryId')))\n            .findFirst();\n\n          if (found.isPresent()) {\n            HashMap ar = found.get();\n            if (ar.get('status').equals('DELETED')) {\n              ar.put('status', 'NORMAL');\n              ar.put('userId', 'model_test');\n              ar.put('appId', 'DEVELOPMENT_BACKEND');\n              ar.put('updatedAt', '2020-05-07T11:56:48.759Z');\n            } else {\n              ctx.op = 'none';\n            }\n          } else {\n            ctx._source.articleCategories.add(params.articleCategory);\n            ctx._source.normalArticleCategoryCount = ctx._source.articleCategories.stream().filter(\n              ar -> ar.get('status').equals('NORMAL')\n            ).count();\n          }\n        ",
                  "lang": "painless",
                  "caused_by": {
                    "type": "null_pointer_exception",
                    "reason": null
                  }
                }
              },
              "status": 400
            },
            "statusCode": 400,
            "headers": {
              "content-type": "application/json; charset=UTF-8",
              "content-length": "1401"
            },
            "warnings": null,
            "meta": {
              "context": null,
              "request": {
                "params": {
                  "method": "POST",
                  "path": "/articles/doc/zzyt710qo56u/_update",
                  "body": "{\"script\":{\"source\":\"\\n          def found = ctx._source.articleCategories.stream()\\n            .filter(ar -> ar.get('categoryId').equals(params.articleCategory.get('categoryId')))\\n            .findFirst();\\n\\n          if (found.isPresent()) {\\n            HashMap ar = found.get();\\n            if (ar.get('status').equals('DELETED')) {\\n              ar.put('status', 'NORMAL');\\n              ar.put('userId', 'model_test');\\n              ar.put('appId', 'DEVELOPMENT_BACKEND');\\n              ar.put('updatedAt', '2020-05-07T11:56:48.759Z');\\n            } else {\\n              ctx.op = 'none';\\n            }\\n          } else {\\n            ctx._source.articleCategories.add(params.articleCategory);\\n            ctx._source.normalArticleCategoryCount = ctx._source.articleCategories.stream().filter(\\n              ar -> ar.get('status').equals('NORMAL')\\n            ).count();\\n          }\\n        \",\"lang\":\"painless\",\"params\":{\"articleCategory\":{\"userId\":\"model_test\",\"appId\":\"DEVELOPMENT_BACKEND\",\"aiModel\":\"bert\",\"aiConfidence\":1,\"positiveFeedbackCount\":0,\"negativeFeedbackCount\":0,\"categoryId\":\"mD2n7nEBrIRcahlYLAr7\",\"status\":\"NORMAL\",\"createdAt\":\"2020-05-07T11:56:48.759Z\",\"updatedAt\":\"2020-05-07T11:56:48.759Z\"}}},\"_source\":[\"articleCategories.*\"]}",
                  "querystring": "",
                  "headers": {
                    "User-Agent": "elasticsearch-js/6.8.6 (linux 4.4.0-137-generic-x64; Node.js v12.14.1)",
                    "Content-Type": "application/json",
                    "Content-Length": "1269"
                  },
                  "timeout": 30000
                },
                "options": {
                  "warnings": null
                },
                "id": 89085
              },
              "name": "elasticsearch-js",
              "connection": {
                "url": "http://cofacts-db-staging:9200/",
                "id": "http://cofacts-db-staging:9200/",
                "headers": {},
                "deadCount": 0,
                "resurrectTimeout": 0,
                "_openRequests": 0,
                "status": "alive",
                "roles": {
                  "master": true,
                  "data": true,
                  "ingest": true,
                  "ml": false
                }
              },
              "attempts": 0,
              "aborted": false
            }
          }
        }
      },
      "authError": false
    }
  ],
  "data": {
    "CreateArticleCategory": null
  }
}

which can be reproduced by this mutation:

mutation {
  CreateArticleCategory(
    articleId: "zzyt710qo56u"
    categoryId: "mD2n7nEBrIRcahlYLAr7"
    aiModel: "bert"
    aiConfidence: 1.0
  ) {
    articleId
    categoryId
  }
}
MrOrz commented 4 years ago

Deploying to production

1st round

16:59 ≎ ~/workspace/rumors-db ➠ npx babel-node db/migrations/202005-add-article-categories.js
ResponseError: Response Error
    at IncomingMessage.<anonymous> (/Users/mrorz/workspace/rumors-db/node_modules/@elastic/elasticsearch/lib/Transport.js:296:25)
    at IncomingMessage.emit (events.js:205:15)
    at IncomingMessage.EventEmitter.emit (domain.js:471:20)
    at endReadableNT (_stream_readable.js:1137:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:9) {
  name: 'ResponseError',
  meta: {
    body: {
      took: 4641,
      timed_out: false,
      total: 36068,
      updated: 3577,
      deleted: 0,
      batches: 4,
      version_conflicts: 423,
      noops: 0,
      retries: [Object],
      throttled_millis: 0,
      requests_per_second: -1,
      throttled_until_millis: 0,
      failures: [Array]
    },
    statusCode: 409,
    headers: {
      'content-type': 'application/json; charset=UTF-8',
      'content-length': '133674'
    },
    warnings: null,
    meta: {
      context: null,
      request: [Object],
      name: 'elasticsearch-js',
      connection: [Object],
      attempts: 1,
      aborted: false
    }
  }
}

2nd round

18:20 ≎ ~/workspace/rumors-db ➠ npx babel-node db/migrations/202005-add-article-categories.js
{
  body: {
    took: 3650,
    timed_out: false,
    total: 36068,
    updated: 0,
    deleted: 0,
    batches: 37,
    version_conflicts: 0,
    noops: 36068,
    retries: { bulk: 0, search: 0 },
    throttled_millis: 0,
    requests_per_second: -1,
    throttled_until_millis: 0,
    failures: []
  },
  statusCode: 200,
  headers: {
    'content-type': 'application/json; charset=UTF-8',
    'content-length': '239'
  },
  warnings: null,
  meta: {
    context: null,
    request: { params: [Object], options: [Object], id: 1 },
    name: 'elasticsearch-js',
    connection: {
      url: 'http://localhost:62222/',
      id: 'http://localhost:62222/',
      headers: {},
      deadCount: 0,
      resurrectTimeout: 0,
      _openRequests: 0,
      status: 'alive',
      roles: [Object]
    },
    attempts: 0,
    aborted: false
  }
}