CLOVER🍀

That was when it all began.

Infinispan Server 11の操作をCLIで行う

これは、なにをしたくて書いたもの?

Infinispan 10以降のServerNGになってCLIが大きく変わっているのですが、1度ゆっくり見てみようかなと思いまして。

ドキュメント的には、こちらですね。

Using the Infinispan Command Line Interface

環境

今回は、Infinispan 11.0.1.Finalを使用しています。

$ bin/cli.sh version
CLI: Infinispan 'Corona Extra' 11.0.1.Final

スクリプトとヘルプ

Infinispan ServerのCLIは、「bin/cli.sh」というファイルになります。

ざっくり見た感じ

  • Infinispan Serverに関するユーザーの管理
  • Cacheの管理
  • Counterの管理
  • Taskの実行
  • Infinispan Serverの停止

あたりができるようです。

WildFlyベースだった頃よりも、現時点では扱える範囲が狭まっている気はします。

Endpointや認証に関する設定などは、直接設定ファイルを編集することになりそうですね。

オプションを見てみましょう。

$ bin/cli.sh -h
Usage: cli [<options>]


Options:
  -t, --truststore           A truststore to use when connecting to SSL/TLS-enabled servers
  -s, --truststore-password  The password for the truststore
  -v, --version              Shows version information
  --trustall                 Whether to trust all certificates
  -f, --file                 File for batch mode
  -c, --connect              A connection URL. Use '-' to connect to http://localhost:11222
  -h, --help

cli commands:
    add         Adds/subtracts a value to/from a counter
    cache       Selects a cache
    cas         Compares and sets counter values
    cd          Selects a subsystem or item
    clear       Clears the screen
    clearcache  Clears the cache
    connect     Connects to a remote server
    container   Selects a container
    counter     Selects counters
    create      Creates a cache or a counter
    describe    Displays information about the specified resource
    disconnect  Disconnects from a remote server
    drop        Drops a cache or a counter
    echo        Echoes messages to the output. Useful for adding information to batch runs.
    encoding    Gets/sets the current encoding
    get         Gets an entry from the cache
    ls          Lists resources in a path
    patch       Patch operations
    put         Puts an entry into the cache
    query       Queries a cache
    remove      Removes an entry from the cache
    replace     Puts an entry into the cache
    reset       Resets a counter to its initial value
    run         Reads and executes commands from one or more files
    schema      Manipulates protobuf schemas
    shutdown    Shuts down individual servers or the entire cluster
    site        Manages backup sites
    user        User operations
    version     Shows version information

コマンドに対してのヘルプも見ることができます。

$ bin/cli.sh user -h
Usage: cli user [<options>]
User operations

Options:
  -h, --help

user commands:
    create       Creates a user
    describe     Describes a user
    remove       Removes a user
    password     Changes a user's password
    groups       Sets a user's groups
    ls           Lists all users/groups
    encrypt-all  Encrypts all of the passwords in a property file.

サブコマンドを持つコマンドに対しては、サブコマンドを指定した状態でヘルプを見ることもできます。

$ bin/cli.sh user create -h
Usage: cli user create [<options>] <username>
Creates a user

Options:
  -p, --password     The password for the user
  -r, --realm        The realm 
  -a, --algorithms   The algorithms used to encrypt the password
  -g, --groups       The groups the user should belong to
  --plain-text       Whether the password should be stored in plain text (not recommended)
  -f, --users-file   The path of the users.properties file
  -w, --groups-file  The path of the groups.properties file
  -s, --server-root  The server root
  -h, --help

Argument:
                   The username for the user

ユーザーを作成する

Infinispan Serverで利用する、ユーザーの作成について。ドキュメントはこちらです。

Adding Infinispan Credentials

オプションはいろいろあるのですが、今回はユーザー名(ispn-admin)とパスワードを指定して作成。

$ bin/cli.sh user create ispn-admin -p password

Connecting to Infinispan Servers

ユーザーを削除したり、パスワードを変更することも可能なようです。

$ bin/cli.sh user -h
Usage: cli user [<options>]
User operations

Options:
  -h, --help

user commands:
    create       Creates a user
    describe     Describes a user
    remove       Removes a user
    password     Changes a user's password
    groups       Sets a user's groups
    ls           Lists all users/groups
    encrypt-all  Encrypts all of the passwords in a property file.

Infinispan Serverに接続する

作成したユーザーを使用して、Infinispan Serverに接続してみましょう。

Connecting to Infinispan Servers

CLIを実行して「connect」コマンドを実行すると、ユーザー名とパスワード聞かれるので入力します。

$ bin/cli.sh
[disconnected]> connect
Username: ispn-admin
Password: ********
[infinispan-server1-17502@cluster//containers/default]>

これで、Infinispan Serverに接続できます。

CLI起動時に、「-c」オプションでconnectを同時に行うこともできます。この時、「-」を指定するとデフォルトの接続先である
http://localhost:11222」を指定したことになります。

$ bin/cli.sh -c -
# 以下も可
## $ bin/cli.sh -c localhost:11222
## $ bin/cli.sh -c http://localhost:11222
Username: ispn-admin
Password: 
[infinispan-server1-17502@cluster//containers/default]>

さらに、接続時にユーザー名とパスワードも指定する場合は、ホスト名の前に含める形になります。

$ bin/cli.sh -c http://ispn-admin:password@localhost:11222
[infinispan-server1-17502@cluster//containers/default]>

接続後は、タブ補完が聞くようになります。

[infinispan-server1-17502@cluster//containers/default]> 
add         cas         clearcache  counter     disconnect  encoding    help        migrate     query       reset       server      stats       user        
alias       cd          connect     create      drop        export      logging     patch       quit        run         shutdown    task        version     
cache       clear       container   describe    echo        get         ls          put         remove      schema      site        unalias 

「help」コマンドでも、利用可能なコマンドを見ることはできますが。

[infinispan-server1-17502@cluster//containers/default]> help
Call `help <command>` where command is one of:
container   replace     evict       export      query       abort       shutdown    server      cas         end         clear       site        user        
disconnect  echo        patch       add         encoding    logging     migrate     roles       clearcache  connect     start       quit        
ls          run         stats       unalias     version     reset       drop        revoke      get         rollback    counter     describe    
commit      put         create      cache       task        begin       schema      remove      alias       cd          help        grant       

Cacheを作成・削除する

Cacheの作成、削除をやってみましょう。

ドキュメントは、こちら。

Creating Caches with the Infinispan Command Line Interface (CLI)

1番簡単なのは、テンプレートを指定してCacheを作成することです。

[infinispan-server1-17502@cluster//containers/default]> create cache myCache -t org.infinispan.DIST_SYNC

使えるCacheのテンプレートは、こちらですね。

https://github.com/infinispan/infinispan/blob/11.0.1.Final/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/DefaultTemplate.java#L10-L17

実際の定義は、こちらになります。

https://github.com/infinispan/infinispan/blob/11.0.1.Final/server/runtime/src/main/resources/infinispan-defaults.xml

作成したCacheはlsで見たり、

[infinispan-server1-17502@cluster//containers/default]> ls caches
___script_cache
myCache
___protobuf_metadata

describeで定義を確認したりできます。

[infinispan-server1-17502@cluster//containers/default]> describe caches/myCache
{
  "distributed-cache" : {
    "mode" : "SYNC",
    "remote-timeout" : 17500,
    "state-transfer" : {
      "timeout" : 60000
    },
    "transaction" : {
      "mode" : "NONE"
    },
    "locking" : {
      "concurrency-level" : 1000,
      "acquire-timeout" : 15000,
      "striping" : false
    },
    "statistics" : true
  }
}

ところで、server/conf/infinispan.xmlを見てもCacheの定義は追加されていません。

   <cache-container name="default" statistics="true">
      <transport cluster="${infinispan.cluster.name}" stack="${infinispan.cluster.stack:tcp}" node-name="${infinispan.node.name:}"/>
   </cache-container>

CLIで作成したCacheの定義は、こちらに書き込まれます。
server/data/caches.xml

<?xml version="1.0" ?>

<infinispan>
    <cache-container>
        <distributed-cache mode="SYNC" remote-timeout="17500" name="myCache" statistics="true">
            <locking concurrency-level="1000" acquire-timeout="15000" striping="false"/>
            <transaction mode="NONE"/>
            <state-transfer timeout="60000"/>
        </distributed-cache>
    </cache-container>

このCacheの定義は、クラスタに参加している全ノードに反映されますし、あとからクラスタに参加したノードにも反映されます。

もうひとつの作り方は、Cacheの定義をファイルで作成し、これを指定してCacheを作成します。

XML Configuration

たとえば、こんなファイルを用意します。
cache-snippet.xml

<?xml version="1.0" ?>
<infinispan>
    <cache-container>
        <distributed-cache mode="SYNC" name="cache">
            <transaction mode="NON_XA"/>
        </distributed-cache>
    </cache-container>
</infinispan>

XMLの要素は、infinispanとcache-container、そしてCacheの定義がひとつになります。

こちらを、「-f」オプションで指定しつつ、Cacheの名前を指定してCacheを作成します。

[infinispan-server1-17502@cluster//containers/default]> create cache transactionalCache -f ./cache-snippet.xml

作成するファイルに複数のCacheを書くには書けるのですが、最後に定義したCacheしか見ていないようです。

また、Cacheの名前は必ず指定する必要があり、用意するCacheに定義された名前は無視され、create cache時に指定した
名前がCacheに反映されます。テンプレートをファイルとして用意した感じなんでしょうね。

Cacheの要素としてどのようなものが定義できるかは、こちらを見るとよいでしょう。

urn:infinispan:config:11.0

Cacheの削除。

Deleting Caches

[infinispan-server1-17502@cluster//containers/default]> drop cache myCache

バッチ操作

バッチ操作を見てみます。

Performing Batch Operations

バッチ操作には、ファイルを入力にする方法と、インタラクティブに入力する方法があります。が、インタラクティブ
入力する方は終了が微妙だったので、ファイルを使う方だけにしておきます。

Performing Batch Operations with Files

こんな感じで、実行するコマンドを列挙したファイルを用意します。
commands.txt

create cache myCache -t org.infinispan.DIST_SYNC
create cache transactionalCache -f ./cache-snippet.xml
ls caches

あとは、CLIに「-f」オプションで指定して実行するだけです。

$ bin/cli.sh -c http://ispn-admin:password@localhost:11222 -f commands.txt

ファイルを作らずにやるなら、こんな感じでしょうか。

$ echo '
create cache myCache -t org.infinispan.DIST_SYNC
create cache transactionalCache -f ./cache-snippet.xml
ls caches
' | bin/cli.sh -c http://ispn-admin:password@localhost:11222 -f -

インタラクティブに実行する方法を、少し変えたものです。

Performing Batch Operations Interactively

ちなみに、今回は「-c」にユーザー名とパスワードを指定しましたが、指定しなかった場合はコマンド実行時に対話的に
聞かれることになります。

$ bin/cli.sh -c - -f commands.txt 
Username: ispn-admin
Password: 

ノードやクラスタを停止する

「shutdown」コマンドで、ノードやクラスタを停止することができます。

特定のノードを指定して、そのノードを停止します。

[infinispan-server1-17502@cluster//containers/default]> shutdown server infinispan-server1-17502

クラスタを停止。クラスタに参加している全ノードが停止します。

[infinispan-server1-17502@cluster//containers/default]> shutdown cluster

起動や再起動はできなさそうですが…。