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

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