【问题标题】:Google App Engine - Difference between local Dev server and deployed serverGoogle App Engine - 本地开发服务器和部署服务器之间的区别
【发布时间】:2015-08-04 00:51:18
【问题描述】:

我正在 Google App Engine 上编写一个应用程序,并且我已经能够编写在本地运行并将数据放入本地数据存储区的代码。但是,当我将代码部署到谷歌服务器时,没有数据放入数据存储区。我的主要指标是:一,当我进入开发者控制台时,我被告知没有条目,二,当我运行获取数据的页面时,没有返回任何内容。

我是否需要在 app.yaml 或开发者控制台中定义新的 Kinds 才能在生产数据存储中工作?

这是我在数据存储中的 put 和 get 方法。同样,在测试环境中工作,在生产环境中不工作,一切都编译得很好。

package tweetdata

import (
    "net/url"
    "time"

    "golang.org/x/net/context"
    "google.golang.org/appengine/datastore"
    "google.golang.org/appengine/log"

    "github.com/ChimeraCoder/anaconda"
)

const linkTweetKind string = "LinkTweet"
const tweetKey string = "Tweets"
const tweetKeyID string = "default_tweetstore"

//LinkTweet contains the address extracted from a tweet and the original tweet
type LinkTweet struct {
    Address *url.URL
    Tweet   anaconda.Tweet
}

//StoreTweet is a struct used for storing a tweet in the datastore
type StoreTweet struct {
    Address     string
    Text        string
    TweetID     int64
    CreatedTime time.Time
    Retweets    int
    Favorites   int
}

//TweetScore is a struct that shows the relative score of an address based on
// it's populatrity
type TweetScore struct {
    Address    *url.URL
    score      int
    lastActive time.Time
}

//WriteLinkTweet writes a given Tweet to the datastore
func WriteLinkTweet(tweet LinkTweet, c context.Context) error {
    log.Infof(c, "Putting Tweet into datastore: %v", tweet.Tweet.Id)
    key := datastore.NewIncompleteKey(c, linkTweetKind, getTweetKey(c))
    created, _ := tweet.Tweet.CreatedAtTime()
    store := &StoreTweet{Address: tweet.Address.String(),
        Text:        tweet.Tweet.Text,
        TweetID:     tweet.Tweet.Id,
        CreatedTime: created,
        Retweets:    tweet.Tweet.RetweetCount,
        Favorites:   tweet.Tweet.FavoriteCount,
    }

    err := datastore.RunInTransaction(c, func(c context.Context) error {
        _, err := datastore.Put(c, key, store)
        log.Errorf(c, "Failed to write LinkTweet to datastore. %v", err.Error())
        return err
    }, nil)
    return err
}

//GetAllNewTweets queries the datastore and gets all tweets created since the last
// time given
func GetAllNewTweets(since time.Time, c context.Context) []StoreTweet {
    q := datastore.NewQuery(linkTweetKind).Ancestor(getTweetKey(c)).Filter("CreatedTime >=", since)
    out := make([]StoreTweet, 0, 15)
    q.GetAll(c, &out)
    return out
}

// guestbookKey returns the key used for all guestbook entries.
func getTweetKey(c context.Context) *datastore.Key {
    // The string "default_guestbook" here could be varied to have multiple guestbooks.
    return datastore.NewKey(c, tweetKey, tweetKeyID, 0, nil)
}

【问题讨论】:

  • 发布您用于(尝试)将实体写入数据存储区的代码。另外,您看到(或未看到)什么让您相信没有任何东西可以得救?
  • 任何非零err的日志记录?
  • 对于您的日志记录,您需要使用来自有效请求上下文(appengine.Context 中的文档)的日志记录函数 - Debugf、Infof、Errorf、Warningf、Criticalf 来登录 App Engine .在您的开发服务器中,您将在终端输出中看到这些日志。在生产 App Engine 中,这些日志存储在您的项目的 Google Cloud Console 中的 Monitoring > Logs 下。
  • 发布 getTweetKey 或将父级设置为 nil 并重试。
  • 由于 Google App Engine 没有提供标准的 http.Transport,因此有必要告诉 Anaconda 使用不同的客户端上下文。我假设您正在这样做,否则它可能也无法在开发服务器上运行。

标签: google-app-engine go google-cloud-datastore


【解决方案1】:

您会看到“最终一致性”的(通常最初令人困惑的)效果。在事务之外写入的实体需要一段时间才能对查询可见。

https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/ 解释了这个问题。 https://cloud.google.com/appengine/docs/go/datastore/transactions 是专门去的。

【讨论】:

  • 那么,添加交易还是等待它最终会在那里?
  • @MrWizard54 - 你要等多久才能看到数据?即使最终保持一致性,数据通常在一秒钟内就可以被查询看到。
  • 通常,但延迟尾巴有时会变长。
  • 此时我运行 Put() 已经超过两个小时,但仍然看不到任何数据。
  • 您使用管理控制台数据存储查看器看到什么了吗?
【解决方案2】:

您是否在StoreTweet 结构中的任何字段上使用datastore:,noindex?为了对任何字段运行任何查询,数据存储必须首先索引您要查询的字段。因此,不索引这些字段将导致查询返回 nil 而没有任何错误。第一次运行查询后,您的开发服务器是否会自动在您的项目中创建 index.yaml 文件?生产数据存储使用这个文件来决定哪些字段应该被索引,所有没有被索引的字段都不能被查询。根据您提供的信息,我能想到的就是这会导致您的查询返回空。

为了进一步调试,我会浏览您的生产数据存储并确保您的数据结构正确,并且值设置为您期望的值。

【讨论】:

  • 开发服务器正在创建 index.yaml - 种类:LinkTweet 祖先:是 属性:-名称:CreatedTime
猜你喜欢
  • 1970-01-01
  • 2016-12-18
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
相关资源
最近更新 更多