1

我有以下非常简单的代码来将字符串保留在datastore. 我从各种datastore例子中总结出来,但我仍然对它不满意。

它的目的只是将一个字符串存储在一个键下persist并在 下检索它fromPresistence

// Used to store the string value.
type Entity struct {
    Value string
}

// Grow my key.
func key(x string) *datastore.Key {
    return datastore.NewKey(context, "Persist", x, 0, nil)
}

// Get it from persistence storage.
func fromPersistence(x string) string {
    var persisted string = x
    // Make my key.
    k := key(x)
    // New entity for filling in.
    e := new(Entity)
    // Look it up!
    if err := datastore.Get(context, k, e); err == nil {
        // It was there!
        persisted = e.Value
        context.Debugf("Persisted %s=%s", x, persisted)
    }

    return persisted
}

// Persist the latest number.
func persist(x string) func(*big.Int) {
    return func(n *big.Int) {
        // Make my key.
        k := key(x)
        // New entity for filling in.
        e := new(Entity)
        // Value is the decimal form of the number.
        e.Value = n.String()
        context.Debugf("Persist %s=%s", start, e.Value)
        if _, err := datastore.Put(context, k, e); err != nil {
            context.Debugf("Persist failed! %s", err)
        }
    }
}

但似乎每次都落后一个。以下是一些日志结果:

C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO     2013-10-15 20:55:08,296 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO     2013-10-15 20:55:09,726 api_server.py:138] Starting API server at: http://localhost:50218
INFO     2013-10-15 20:55:09,746 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO     2013-10-15 20:55:09,763 admin_server.py:117] Starting admin server at: http://localhost:8000
INFO     2013-10-15 20:56:22,131 module.py:599] default: "GET / HTTP/1.1" 304 -
INFO     2013-10-15 20:56:22,344 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
INFO     2013-10-15 20:56:22,448 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:26 DEBUG: Persisted 38913371956013078496870267859=3378577588146889866220112993
2013/10/15 20:56:26 DEBUG: Persist 38913371956013078496870267859=21186844412818184262771263024
...
2013/10/15 20:56:30 DEBUG: Persist 38913371956013078496870267859=1324177775801136516423203939
INFO     2013-10-15 20:56:30,756 module.py:599] default: "GET /n HTTP/1.1" 200 19
INFO     2013-10-15 20:56:30,927 module.py:599] default: "GET /favicon.ico HTTP/1.1" 304 -
2013/10/15 20:56:32 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609

C:\Go\GAE\go_appengine\google\src>\go\gae\go_appengine\dev_appserver.py unique/
INFO     2013-10-15 20:57:15,657 sdk_update_checker.py:245] Checking for updates to the SDK.
INFO     2013-10-15 20:57:17,033 api_server.py:138] Starting API server at: http://localhost:50241
INFO     2013-10-15 20:57:17,085 dispatcher.py:168] Starting module "default" running at: http://localhost:8080
INFO     2013-10-15 20:57:17,098 admin_server.py:117] Starting admin server at: http://localhost:8000
2013/10/15 20:57:24 DEBUG: Persisted 38913371956013078496870267859=1324177775801136516423203939
2013/10/15 20:57:24 DEBUG: Persist 38913371956013078496870267859=20778614526287997725322370609
...

查看最后一次持久性尝试如何存储20778614526287997725322370609但检索尝试返回之前的持久值1324177775801136516423203939

我究竟做错了什么?

注意:我已更改代码以使用context.Debugf打印调试字符串的机制来排除奇怪的日志记录。

Logf代码如下。我确信这是奇怪日志条目的原因。这不是我的问题的目标。我会自己解决这个问题。

func Logf(format string, a ...interface{}) {
    if context != nil {
        // Context is valid.
        if len(logQueue) > 0 {
            // Roll out the stored entries.
            for i := 0; i < len(logQueue); i++ {
                context.Debugf("%s", logQueue[i])
            }
            // Empty the queue.
            logQueue = make([]string, 0)
        }
        // Pass a "" to just flush the queue
        if format != "" {
            // Log it through the context.
            context.Debugf(format, a)
        }
    } else {
        // No context! Queue it up.
        logQueue = append(logQueue, fmt.Sprintf(format, a...))
    }

}
4

2 回答 2

0

查看您的数据存储代码...它以某种方式将值与键一起存储。

该声明

Logf("Persist %s=%s", x, e.Value)

正在生产:

2013/10/13 23:21:26 DEBUG: Persist [38913371956013078496870267859 19124562091635092830747528895]=%s(MISSING)

这意味着您的“x”值有一部分键和其他一些值(从您的文本来看,它似乎是前一个键值)。

缺少实际值...这就是%s(MISSING)位的含义

于 2013-10-14T04:09:01.040 回答
0

您在这里很有可能看到的是分布式系统的那些意想不到的方面之一。如果您对应用程序的不同实例的调用persistfromPersistence最终在不同实例上运行,则日志消息可能会采用稍微不同的路径到达它们的持久位置。请注意,看似颠倒的日志消息上的时间戳是相同的。

于 2013-10-14T01:17:21.823 回答