`

kestreldocsguide

 
阅读更多
A working guide tokestrel

Kestrel is a very simple message queue that runs on the JVM. Itsupports multiple protocols:

A single kestrel server has a set of queues identified by aname, which isalso the filename of that queue's journal file(usually in/var/spool/kestrel). Each queue is astrictly-ordered FIFO of "items" ofbinary data. Usually this datais in some serialized format like JSON orruby's marshal format.

Generally queue names should be limited to alphanumerics[A-Za-z0-9], dash(-) and underline(_). In practice, kestrel doesn't enforceanyrestrictions other than the name can't contain slash(/) because that can'tbe used in filenames, squiggle(~) because it's used for temporary files,plus(+) because it's used for fanout queues, and dot(.) because it'sreserved for future use. Queue namesare case-sensitive, but if you're runningkestrel on OS X orWindows, you will want to refrain from taking advantage ofthis,since the journal filenames on those two platforms arenotcase-sensitive.

A cluster of kestrel servers is like a memcache cluster: theservers don'tknow about each other, and don't do anycross-communication, so you can add asmany as you like. Clientshave a list of all servers in the cluster, and pickone at randomfor each operation. In this way, each queue appears to be spreadoutacross every server, with items in a loose ordering.

When kestrel starts up, it scans the journal folder and createsqueues basedon any journal files it finds there, to restore stateto the way it was whenit last shutdown (or was killed or died). Newqueues are created by referringto them (for example, adding ortrying to remove an item). A queue can bedeleted with the "delete"command.

Configuration

The config files for kestrel are scala expressions loaded atruntime, usuallyfrom production.scala, although youcan use development.scala bypassing-Dstage=development to the java commandline.

The config file evaluates to a KestrelConfig objectthat's used to configurethe server as a whole, a default queue, andany overrides for specific namedqueues. The fields onKestrelConfig are documented here with theirdefaultvalues:

To confirm the current configuration of each queue, send"dump_config" toa server (which can be done over telnet).

To reload the config file on a running server, send "reload" thesame way.You should immediately see the changes in "dump_config",to confirm. Reloadingwill only affect queue configuration,中云融信, notglobal server configuration. Tochange the server configuration,restart the server.

Logging is configured according to util-logging.The logging configurationsyntax is described here:

Per-queue configuration is documented here:

Queue alias configuration is documented here:

Full queues

A queue can have the following limits set on it:

If either of these limits is reached, no new items can be addedto the queue.(Clients will receive an error when trying to add.) Ifyou setdiscardOldWhenFull to true, then all adds willsucceed, and the oldestitem(s) will be silently discarded until thequeue is back within the itemand size limits.

maxItemSize limits the size of any individual item.If an add is attemptedwith an item larger than this limit, italways fails.

The journal file

The journal file is the only on-disk storage of a queue'scontents, and it'sjust a sequential record of each add or removeoperation that's happened onthat queue. When kestrel starts up, itreplays each queue's journal to buildup the in-memory queue that ituses for client queries.

The journal file is rotated in one of two conditions:

For example, if defaultJournalSize is 16MB (thedefault), then if the queueis empty and the journal is larger than16MB, it will be truncated into a new(empty) file. If the journalis larger than maxJournalSize (1GB by default),thejournal will be rewritten periodically to contain just the liveitems.

You can turn the journal off for a queue(keepJournal = false) and the queuewill exist only inmemory. If the server restarts, all enqueued items arelost. You canalso force a queue's journal to be sync'd to disk periodically,oreven after every write operation, at a performance cost,usingsyncJournal.

If a queue grows past maxMemorySize bytes (128MB bydefault), only thefirst 128MB is kept in memory. The journal isused to track later items, andas items are removed, the journal isplayed forward to keep 128MB in memory.This is usually known as"read-behind" mode, but Twitter engineers sometimesrefer to it asthe "square snake" because of the diagram used to brainstormtheimplementation. When a queue is in read-behind mode, removing anitem willoften cause 2 disk operations instead of one: one torecord the remove, andone to read an item in from disk to keep128MB in memory. This is thetrade-off to avoid filling memory andcrashing the JVM.

Item expiration

When they come from a client, expiration times are handled inthe same way asmemcache: if the number is small (less than onemillion), it's interpreted asa relative number of seconds from now.Otherwise it's interpreted as anabsolute unix epoch time, inseconds since the beginning of 1 January 1970GMT.

Expiration times are immediately translated into an absolutetime, inmilliseconds, and if it's further in the futurethan the queue's maxAge,the maxAge isused instead. An expiration of 0, which is usually thedefault,means an item never expires.

Expired items are flushed from a queue whenever a new item isadded orremoved. Additionally, if the global config optionexpirationTimerFrequency<wbr></wbr>is set,中云融信, a backgroundthread will periodically remove expired items from thehead of eachqueue. The provided production.conf sets this to onesecond.If this is turned off, an idle queue won't have any itemsexpired, but youcan still trigger a check by doing a "peek" onit.

Normally, expired items are discarded. IfexpireToQueue is set, thenexpired items are moved tothe specified queue just as if a client had putit there. The itemis added with no expiration time, but that can beoverridden if thenew queue has a default expiration policy.

To prevent stalling the server when it encounters a swarm ofitems that allexpired at the same time, maxExpireSweeplimits the number of items thatwill be removed by the backgroundthread in a single round. This is primarilyuseful as a throttlingmechanism when using a queue as a way to delay work.

Queue expiration

Whole queues can be configured to expire as well. IfmaxQueueAge issetexpirationTimerFrequency<wbr></wbr> is used to check the queueage. If the queue isempty, and it has been longer thanmaxQueueAge since it was created thenthe queue will bedeleted.

Fanout Queues

If a queue name has a + in it (like"orders+audit"), it's treated as afanout queue, usingthe format +. These queues belong to aparent queue --in this example, the "orders" queue. Every item written intoaparent queue will also be written into each of its children.

Fanout queues each have their own journal file (if the parentqueue has ajournal file) and otherwise behave exactly like anyother queue. You can getand peek and even add items directly to achild queue if you want. It uses theparent queue's configurationinstead of having independent child queueconfiguration blocks.

When a fanout queue is first referenced by a client, the journalfile (if any)is created, and it will start receiving new itemswritten to the parent queue.Existing items are not copied over. Afanout queue can be deleted to stop itfrom receiving new items.

fanoutOnly may be set to true if the queue inquestion will only serve writepoint for fanout queues. No journalfile will be kept for the parent, onlyfor the child queues. Thissaves the overhead of writing to the parent andremoves the need toempty it. Note that setting fanoutOnly to trueandhaving no fanouts for the queue effectively makes it a blackhole.

Queue Aliases

Queue aliases are somewhat similar to fanout queues, but withouta requirednaming convention or implicit creation of child queues. Aqueue alias canonly be used in set operations. Kestrel responds toattempts to retrieveitems from the alias as if it were an emptyqueue. Delete and flush requestsare also ignored.

Protocols

Kestrel supports three protocols: memcache, thrift and text. Thecan be used to connect clientsto a Kestrel server via the memcacheor thrift protocols.

Thrift


The thrift protocol is documented in the thrift IDL:

Reliable reads via the thrift protocol are specified byindicating how long the servershould wait before aborting theunacknowledged read.

Memcache


The official memcache protocol is described here:

The kestrel implementation of the memcache protocol commands isdescribed below.

For example, to open a new read, waiting up to 500msec for anitem:

  GET work/t=500/open

Or to close an existing read and open a new one:

  GET work/close/open

Reliable reads


Note: this section is specific to the memcache protocol.

Normally when a client removes an item from the queue, kestrelimmediatelydiscards the item and assumes the client has takenownership. This isn'talways safe, because a client could crash orlose the network connectionbefore it gets the item. So kestrel alsosupports a "reliable read" thathappens in two stages, using the/open and /close options toGET.

When /open is used, and an item is available,kestrel will remove it fromthe queue and send it to the client asusual. But it will also set the itemaside. If a client disconnectswhile it has an open read, the item is put backinto the queue, atthe head, so it will be the next item fetched. Only oneitem can be"open" per client connection.

A previous open request is closed with /close. Theserver will reject anyattempt to open another read when one isalready open, but it will ignore/close if there's noopen request, so that you can add /close toeveryGET request for convenience.

If for some reason you want to abort a read withoutdisconnecting, you can use/abort. But because aborteditems are placed back at the head of the queue,this isn't a goodway to deal with client errors. Since the error-causing itemwillalways be the next one available, you'll end up bouncing the sameitemaround between clients instead of making progress.

There's always a trade-off: either potentially lose items orpotentiallyreceive the same item multiple times. Reliable readschoose the latter option.To use this tactic successfully, workitems should be idempotent, meaning thework could be done 2 or 3times and have the same effect as if it had beendone only once(except wasting some resources).

Example:

GET dirty_jobs/close/open(receives job 1)GET dirty_jobs/close/open(closes job 1, receives job 2)...etc...

Text protocol


Kestrel supports a limited, text-only protocol. You areencouraged to use thememcache protocol instead.

The text protocol does not support reliable reads.

Server stats

Global stats reported by kestrel are:

For each queue, the following stats are also reported:

Statistics may be retrieved by accessing the on the admin HTTPport.For example:http://kestrel.host:2223/stats.json?period=60.

Statistics are also available via the memcache protocol usingthe STATS command.

Kestrel as a library

You can use kestrel as a library by just sticking the jar onyour classpath.It's a cheap way to get a durable work queue forinter-process or inter-threadcommunication. Each queue isrepresented by a PersistentQueue object:

class PersistentQueue(val name: String, persistencePath: String,                      @volatile var config: QueueConfig, timer: Timer,                      queueLookup: Option[(String => Option[PersistentQueue])]) {

and must be initialized before using:

def setup(): Unit

specifying the path for the journal files (if the queue will bejournaled),the name of the queue, a QueueConfig object(derived from QueueBuilder),a timer for handlingtimeout reads, and optionally a way to find other namedqueues (forexpireToQueue support).

To add an item to a queue:

def add(value: Array[Byte], expiry: Option[Time]): Boolean

It will return false if the item was rejectedbecause the queue was full.

Queue items are represented by a case class:

case class QItem(addTime: Time, expiry: Option[Time], data: Array[Byte], var xid: Int)

and several operations exist to remove or peek at the headitem:

def peek(): Option[QItem]def remove(): Option[QItem]

To open a reliable read, set transaction true, andlater confirm or unremovethe item by its xid:

def remove(transaction: Boolean): Option[QItem]def unremove(xid: Int)def confirmRemove(xid: Int)

You can also asynchronously remove or peek at items usingfutures.

def waitRemove(deadline: Option[Time], transaction: Boolean): Future[Option[QItem]]def waitPeek(deadline: Option[Time]): Future[Option[QItem]]

When done, you should close the queue:

def close(): Unitdef isClosed: Boolean

Here's a short example:

var queue = new PersistentQueue("work", "/var/spool/kestrel", config, timer, None)queue.setup()// add an item with no expiration:queue.add("hello".getBytes, 0)// start to remove it, then back out:val item = queue.remove(true)queue.unremove(item.xid)// remove an item with a 500msec timeout, and confirm it:queue.waitRemove(500.milliseconds.fromNow, true)() match {  case None =>    println("nothing. :(")  case Some(item) =>    println("got: " + new String(item.data))    queue.confirmRemove(item.xid)}queue.close()
分享到:
评论

相关推荐

    node-v18.11.0-headers.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    JavaScript_跨平台3D场景编辑器基于threejs golang和mongodb桌面和web.zip

    JavaScript

    JavaScript_如何编写跨平台Nodejs代码.zip

    JavaScript

    北邮大三物流工程物流信息系统课程设计

    北邮大三物流工程物流信息系统课程设计

    0520_1.mov

    0520_1.mov

    实现CAD基础绘图显示功能(C#源码)

    使用C#开发的,一款非常简单的二维CAD绘图程序。 支持多种图元:线段、构造线、射线、多段线、圆、圆弧、文字。 缩放和平移视图。 支持图层。 图元支持夹点,并且可以通过移动夹点来修改图元。 捕捉。目前支持的捕捉类型有:端点、中点、中心点、象限点。 基本的编辑操作:删除、复制、镜像、偏移、移动。 撤销和重做。 支持点选和框选来选择图元。

    aspectjweaver-1.7.4.jar

    作为AspectJ编译器的一部分,aspectj-weaver.jar主要有以下作用: 切面织入:aspectj-weaver.jar可以将定义好的切面织入到Java应用程序的字节码中,实现横切关注点的模块化aspectjweaver.jar是AspectJ编织器的主要库文件,它提供了AspectJ编织器的核心功能。它可以在编译时或运行时将AspectJ切面(aspects)编织到Java类中,实现面向切面

    JavaScript_使用Meteor构建的开源看板保持变量字段名camelCase对于翻译只添加Pull Request更改

    JavaScript

    JavaScript_JS中最强大的数据验证库.zip

    JavaScript

    node-v14.17.5-headers.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    Android的移动应用下拉通知效果源码.rar

    Android的移动应用下拉通知效果源码.rar

    变电站呼吸器硅胶体破损

    变电站呼吸器硅胶体破损数据集,数据总共106张图片,标注为VOC格式

    JavaScript_Open Web Components指导开发Web组件的工具和库.zip

    JavaScript

    node-v16.19.1-headers.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    JavaScript_每个人都可以在这里贡献.zip

    JavaScript

    移动应用Android 腾讯微博客户端源码.rar

    移动应用Android 腾讯微博客户端源码.rar

    da_1716184227697..apk.1.1.1

    da_1716184227697..apk.1.1.1

    node-v6.1.0-headers.tar.gz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    avalon-framework-api-4.3.1.jar

    avalon-framework-api-4.3.1.jar ======Avalon的组件模型一组联合的经典接口组成,用来定义容器和组件之间交换的物件。容器中的组件的需求使用和组件相关的元信息描述来表示。接口和缺省实现由Avalon ====== jeeplus需要用到的包

    固定资产管理系统.zip

    固定资产管理系统是对高校固定资产的一个信息化管理系统,基本功能包括:对固定资产的购进、接触、销毁,对物品的使用状态、借出状态、库存状态等进行标识,对各类物品进行编号,根据编号进行查询,根据名称进行查询等。本系统结构如下: (1)系统登录: 用户登录模块:登录功能 重置 (2)系统用户管理: 对系统用户的增加 系统用户的权限修改 系统用户的删除 分配系统用户的权限 修改本身登录密码 资产的相关维护 (3)员工信息管理: 教工的增加、修改、删除、查询 (4)资产入库管理: 资产的录入 资产的属性修改 资产的报废删除 资产的属性查询 (5)资产维护管理: 物资的维修、维护物资的信息修改 (7)资产借还管理: 增加借出资产 查询借出资产 归还已借出资产 (8)打印报表

Global site tag (gtag.js) - Google Analytics