Skip to content
This repository has been archived by the owner on Nov 9, 2022. It is now read-only.

Allow execute_query with app_name set to ml.app-name #661

Closed
grtjn opened this issue Sep 16, 2016 · 9 comments
Closed

Allow execute_query with app_name set to ml.app-name #661

grtjn opened this issue Sep 16, 2016 · 9 comments
Assignees
Milestone

Comments

@grtjn
Copy link
Contributor

grtjn commented Sep 16, 2016

I changed execute_query_8 as shown below, and that allows me to at least invoke modules of current project with for instance:

  def replace_addresses
    country = find_arg(["--country"]) || "NL"
    lang = find_arg(["--language"]) || "Dutch"
    logger.info "Replacing customer addresses with #{country}/#{lang}.."
    r = execute_query(
      %Q{
        xquery version "1.0-ml";
        xdmp:invoke('/task/replace-addresses.xqy', (xs:QName("country"), "#{country}", xs:QName("lang"), "#{lang}"))
      },
      { :app_name => @properties["ml.app-name"] }
    )
    r.body = parse_body(r.body)
    logger.info r.body
  end

Patch for server_config.rb:

  def execute_query_8(query, properties = {})
    if properties[:app_name] != nil && properties[:app_name] != @properties["ml.app-name"]
      raise ExitException.new("Executing queries with an app_name (currently) not supported with ML8+")
    end

    headers = {
      "Content-Type" => "application/x-www-form-urlencoded"
    }

    params = {
      :xquery => query,
      :locale => LOCALE,
      :tzoffset => "-18000"
    }

    port = @qconsole_port
    if properties[:app_name] != nil
      port = @properties["ml.app-port"]
    end
    if properties[:db_name] != nil
      params[:database] = properties[:db_name]
    end

    r = go "#{@protocol}://#{@hostname}:#{port}/v1/eval", "post", headers, params

    raise ExitException.new(JSON.pretty_generate(JSON.parse(r.body))) if r.body.match(/\{"error"/)

    r
  end
@grtjn grtjn added the bug label Sep 16, 2016
@grtjn grtjn added this to the 1.7.4 milestone Sep 16, 2016
@grtjn
Copy link
Contributor Author

grtjn commented Sep 16, 2016

I marked it as bug as the missing app_name feature is pretty severe. We could further improve on this, by discovering the rest-port from app_name. I think I did something similar with capturing of app-builder projects, which are rest-based too..

@grtjn
Copy link
Contributor Author

grtjn commented Sep 16, 2016

Even nicer would be having an invoke_query command, which takes modules-uri, and list of name/value pairs. Could make use of /v1/invoke, but would mean ML8+ support only. xcc invoke was never implemented from the looks of it..

@dmcassel dmcassel modified the milestones: 1.7.5, 1.7.4 Oct 26, 2016
jamsilvia added a commit to jamsilvia/roxy that referenced this issue Nov 1, 2016
Added functionality to adjust replica setup when scaling out.  This
includes redistribution of replicas by creating new ones where needed
and marking the old ones for delete.  Additional functions for cleaning
up once the new replicas are sync replication.
@jamsilvia jamsilvia mentioned this issue Nov 1, 2016
@jamsilvia jamsilvia mentioned this issue Nov 15, 2016
jamsilvia added a commit to jamsilvia/roxy that referenced this issue Nov 16, 2016
Updated default replica count to 1, which is the minimum.
Use downcase vs explicit strings.
jamsilvia added a commit to jamsilvia/roxy that referenced this issue Jan 20, 2017
Updated default replica count to 1, which is the minimum.
Use downcase vs explicit strings.
dmcassel added a commit that referenced this issue Jan 23, 2017
@RobertSzkutak RobertSzkutak modified the milestones: 1.7.6, 1.7.5 Jan 30, 2017
@RobertSzkutak RobertSzkutak reopened this Jan 30, 2017
@RobertSzkutak RobertSzkutak modified the milestones: 1.7.6, 1.7.5 Jan 30, 2017
@dmcassel dmcassel modified the milestones: 1.7.6, April 2017 Jan 30, 2017
@grtjn
Copy link
Contributor Author

grtjn commented Feb 22, 2017

The above mentioned patch only works for rest and hybrid projects, and app-level auth might interfere as well. A more robust method is this:

  def execute_query_8(query, properties = {})
    if properties[:app_name] != nil && properties[:app_name] != @properties["ml.app-name"]
      raise ExitException.new("Executing queries with an app_name (currently) not supported with ML8+")
    end

    headers = {
      "Content-Type" => "application/x-www-form-urlencoded"
    }

    params = {
      :locale => LOCALE,
      :tzoffset => "-18000"
    }

    port = @qconsole_port
    if properties[:app_name] != nil
      params[:xquery] = %Q{
        xquery version "1.0-ml";
        let $query := <query><![CDATA[#{query}]]></query>
        return xdmp:eval(
          string($query),
          (),
          <options xmlns="xdmp:eval">
            <database>{xdmp:database("#{@properties["ml.content-db"]}")}</database>
            <modules>{xdmp:database("#{@properties["ml.modules-db"]}")}</modules>
          </options>
        )
      }
    else
      params[:xquery] = query
    end
    if properties[:db_name] != nil
      params[:database] = properties[:db_name]
    end

    r = go "#{@protocol}://#{@hostname}:#{port}/v1/eval", "post", headers, params

    raise ExitException.new(JSON.pretty_generate(JSON.parse(r.body))) if r.body.match(/\{"error"/)

    r
  end

@grtjn
Copy link
Contributor Author

grtjn commented Feb 22, 2017

Tested above with this code:

  def test
    logger.info "Execute query with no params:"
    r = execute_query(
      %Q{
        xquery version "1.0-ml";

        "content-db: " || xdmp:database-name(xdmp:database()),
        "modules-db: " || xdmp:database-name(xdmp:modules-database()),
        "schemas-db: " || xdmp:database-name(xdmp:schema-database()),
        "trigger-db: " || xdmp:database-name(xdmp:triggers-database())
      }
    )
    r.body = parse_body r.body
    logger.info r.body
    logger.info ""

    logger.info "Execute query with db_name param:"
    r = execute_query(
      %Q{
        xquery version "1.0-ml";

        "content-db: " || xdmp:database-name(xdmp:database()),
        "modules-db: " || xdmp:database-name(xdmp:modules-database()),
        "schemas-db: " || xdmp:database-name(xdmp:schema-database()),
        "trigger-db: " || xdmp:database-name(xdmp:triggers-database())
      },
      { :db_name => @properties['ml.content-db'] }
    )
    r.body = parse_body r.body
    logger.info r.body
    logger.info ""

    logger.info "Execute query with app_name param:"
    r = execute_query(
      %Q{
        xquery version "1.0-ml";

        "content-db: " || xdmp:database-name(xdmp:database()),
        "modules-db: " || xdmp:database-name(xdmp:modules-database()),
        "schemas-db: " || xdmp:database-name(xdmp:schema-database()),
        "trigger-db: " || xdmp:database-name(xdmp:triggers-database())
      },
      { :app_name => @properties['ml.app-name'] }
    )
    r.body = parse_body r.body
    logger.info r.body
    logger.info ""
  end

@grtjn
Copy link
Contributor Author

grtjn commented Feb 22, 2017

It should probably also take modules-root into account, but I leave that as exercise..

@grtjn
Copy link
Contributor Author

grtjn commented Feb 22, 2017

We might also want to print a note that it might behave differently than it used to do if using app_name. A call to xdmp:server will return the id of App-Services, not of the app you specified. Not sure how the old xcc eval handled this, but I think it did better..

@grtjn
Copy link
Contributor Author

grtjn commented Apr 24, 2017

Also, the eval negates the database param..

@RobertSzkutak RobertSzkutak modified the milestones: July 2017, April 2017 May 5, 2017
@grtjn
Copy link
Contributor Author

grtjn commented May 28, 2017

Looking back through history, Roxy never used XCC eval, but has always relied on QC for eval in the past. My comment on xdmp:server returning wrong id has always applied, and can be neglected in that sense. It is a valid short-coming of Roxy, but one that has always existed.

And the eval indeed ignores the database param, but that is appropriate. One is supposed to call execute_query with either an app_name or a db_name, not both. If db_name is provided, the xdmp:eval is skipped, and query gets evaluated directly, while taking database param into account..

grtjn added a commit to grtjn/roxy that referenced this issue May 28, 2017
@grtjn grtjn modified the milestones: May 2017, July 2017 May 28, 2017
@grtjn grtjn self-assigned this Jun 1, 2017
grtjn added a commit to grtjn/roxy that referenced this issue Jun 9, 2017
grtjn added a commit to grtjn/roxy that referenced this issue Jun 9, 2017
RobertSzkutak added a commit that referenced this issue Jun 9, 2017
Fixed #661: wrap app_name query in xdmp:eval to provide appropriate context
@grtjn
Copy link
Contributor Author

grtjn commented Jun 9, 2017

Fixed in dev

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants